none
Get a list of the SQL Server DataTypes in C# RRS feed

  • Question

  • I want to be able to convert a string such as "nvarchar" to an instance of SQLDataType. I was going to enumerate through a collection of SQLDataTypes until I find a match, but I cannot seem to programatically get a list of all the datatypes. SQLDataType is an enum, but there is no Enum.GetValues() method. Please help
    Monday, January 21, 2008 7:19 PM

Answers

  • The following shows how to get the names of all of the members of the enumeration:

     

    Code Block

    foreach (string s in Enum.GetNames(typeof(System.Data.SqlDbType)))

    {

    Console.WriteLine(s);

    }

     

     

    Be careful if you are comparing schema information retrieved from the database against this enumeration; some SQL Server types aren't in here.  For example, a column may be declared NUMERIC in SQL.  This is equivalent to DECIMAL; only SqlDbType.Decimal appears in the enumeration.

     

    Tuesday, January 22, 2008 1:31 AM