Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# to SQL Server Type Translation Lookup

C# to SQL Server Type Translation Lookup

Scheduled Pinned Locked Moved C#
helpcsharpdatabasesql-serversysadmin
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    perlmunger
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • P perlmunger

      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

      M Offline
      M Offline
      Mark Smithson
      wrote on last edited by
      #2

      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.

      P 1 Reply Last reply
      0
      • M Mark Smithson

        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.

        P Offline
        P Offline
        perlmunger
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups