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

前端 未结 5 2019
野性不改
野性不改 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 21:52

    You can use enum System.Data.CommandBehavior as paramter for method SqlCommand.ExecuteReader(System.Data.CommandBehavior.KeyInfo):

    SqlCommand cmd = connection.CreateCommand();
    cmd.CommandText = "select column from table";
    SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.KeyInfo);
    SqlDbType type = (SqlDbType)(int)reader.GetSchemaTable().Rows[0]["ProviderType"];
    

    This example looks like the example with using sql: SET FMTONLY ON; SET FMTONLY OFF. But you haven't to use SET FMTONLY. And in this case you don't
    receive data from the table. You receive only metadata.

提交回复
热议问题