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

前端 未结 5 1999
野性不改
野性不改 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

    In SQL Server you can use the FMTONLY option. It allows you to run a query without getting any data, just returning the columns.

    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"];
    

提交回复
热议问题