How to get the SqlType of a column in a DataTable?

前端 未结 7 423
予麋鹿
予麋鹿 2020-12-31 01:45

I have a DataTable obtained from a SQL DataBase, like this:

using (SqlCommand cmd = new SqlCommand(query, _sqlserverDB))
{
    using (SqlDataAdapter adapter          


        
相关标签:
7条回答
  • 2020-12-31 01:57

    Of course it is possible to take SqlDbType of a column, the answer is here on SO: link.

    SqlCommand cmd = connection.CreateCommand();
    cmd.CommandText = "SET FMTONLY ON; select column from table; SET FMTONLY OFF";
    SqlDataReader reader = cmd.ExecuteReader();
    SqlDbType type = (SqlDbType)(int)reader.GetSchemaTable().Rows[0]["ProviderType"];
    
    0 讨论(0)
  • 2020-12-31 01:59

    As David says ... you are in .NET so the types will be .NET types. This is a listing of type mappings from SQL Server to .Net that shows you what .NET type you will end up with for a given Sql column type .. hope this helps ..

    http://msdn.microsoft.com/en-us/library/ms131092.aspx

    0 讨论(0)
  • 2020-12-31 02:01

    You cannot because System.Data.DataTable (or DataColumn, or DataSet, or DataRow...) is a generic .NET data container which works the same way regardless on the specific database engine you loaded your data from.

    this means that provided you used a .NET Connector for SQL Server, MySQL, Access, PostgreSQL or anything else, the DataTable and DataColumn classes are always the same and being ADO.NET objects are generic to work with any db engine, so the columns are typed with the .NET types as you have found out.

    0 讨论(0)
  • 2020-12-31 02:07

    Another approach is to let SQL do the work for you:

    SqlConnection rConn = connectToSQL(); //returns sql connection
    SqlCommand SqlCmd = new SqlCommand();
    SqlCmd = rConn.CreateCommand();
    SqlCmd.CommandText = "SELECT ORDINAL_POSITION, " +
                             "COLUMN_NAME, " +
                             "DATA_TYPE, " +
                             "CHARACTER_MAXIMUM_LENGTH, " +
                             "IS_NULLABLE " +
                        "FROM INFORMATION_SCHEMA.COLUMNS " +
                        "WHERE TABLE_NAME = 'TableName'";
    SqlDataReader SqlDr = SqlCmd.ExecuteReader();
    SqlDr.Read();
    while (SqlDr.Read()) { 
        var OrdPos = SqlDr.GetValue(0);
        var ColName = SqlDr.GetValue(1);
        var DataType = SqlDr.GetValue(2);
        var CharMaxLen = SqlDr.GetValue(3);
        var IsNullable = SqlDr.GetValue(4);
        Console.WriteLine("ColName - " + ColName + " DataType - " + DataType + " CharMaxLen - " + CharMaxLen);
    }
    
    0 讨论(0)
  • 2020-12-31 02:12
    SqlConnection SqlCon = new SqlConnection("Data Source=(local);Database=  dbname;Integrated Security=SSPI;");
    
    SqlDataReader SqlDr;
    
    SqlCon.Open();
    SqlCmd = SqlCon.CreateCommand();
    
    SqlCmd.CommandText = "select * from Tablename";
    
    SqlDr = SqlCmd.ExecuteReader();
    SqlDr.Read();
    int i=0;
    while (i < SqlDr.FieldCount)
    { MessageBox.Show(SqlDr.GetDataTypeName(i)); i++;}'
    
    0 讨论(0)
  • 2020-12-31 02:17

    If you are using DataReader -

    SqlDataReader reader = cmd.ExecuteReader(); reader.GetDataTypeName(int ordinal)

    should work if you want the SQL data type of a column

    0 讨论(0)
提交回复
热议问题