How do I get the SqlDbType of a column in a table using ADO.NET?

前端 未结 5 1983
野性不改
野性不改 2020-12-31 21:45

I\'m trying to determine at runtime what the SqlDbType of a sql server table column is.

is there a class that can do that in System.Data.SqlClient or should I do the

5条回答
  •  一生所求
    2020-12-31 22:12

    If you are eventually going to read the data, you can do this:

    SqlCommand comm = new SqlCommand("SELECT * FROM Products", connection);
    using (SqlDataReader reader = comm.ExecuteReader())
    {
        while (reader.Read())
        {
            Type type = reader.GetSqlValue(0).GetType();
            // OR Type type = reader.GetSqlValue("name").GetType();
            // yields type "System.Data.SqlTypes.SqlInt32"
        }
    }
    

提交回复
热议问题