C# to SQL Server Type Translation Lookup
-
I am using SQLDMO to build a code generation application. When I find a column type in a given table, it usually comes back with the name 'int' for an integer, 'varchar' for a VarChar etc. The problem is that I need to do a translation from that sql type to the approrpriate SqlDbType in C# (e.g. SqlDbType.Int, SqlDbType.VarChar), so that I can output some source code that will build with no further modification. I, obviously, could just ignore the case difference and make the change manually to the generated code, but it seems there should be a way to avoid this. Now, of course, I could create my own hashtable and hard code the mapped values (e.g. 'int' -> 'Int', 'varchar' -> 'VarChar', etc.), but it occured to me that this may already be in either the framework or the SQLDMO. Is anyone aware of a way to make this translation using a built-in mechanism? Or am I stuck creating a hard-coded map? TIA. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
-
I am using SQLDMO to build a code generation application. When I find a column type in a given table, it usually comes back with the name 'int' for an integer, 'varchar' for a VarChar etc. The problem is that I need to do a translation from that sql type to the approrpriate SqlDbType in C# (e.g. SqlDbType.Int, SqlDbType.VarChar), so that I can output some source code that will build with no further modification. I, obviously, could just ignore the case difference and make the change manually to the generated code, but it seems there should be a way to avoid this. Now, of course, I could create my own hashtable and hard code the mapped values (e.g. 'int' -> 'Int', 'varchar' -> 'VarChar', etc.), but it occured to me that this may already be in either the framework or the SQLDMO. Is anyone aware of a way to make this translation using a built-in mechanism? Or am I stuck creating a hard-coded map? TIA. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
If all the values match, except for case then you could try using Enum.Parse() to get the correct enum value and then outputting the name into the code you are generating.
-
If all the values match, except for case then you could try using Enum.Parse() to get the correct enum value and then outputting the name into the code you are generating.
Very nice!! Thank you. That seems to work. I haven't tried all of the data types yet, but it looks pretty good. Thanks again. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall