如何在.net / c#中获取查询结果的底层SQL类型

本文关键字:结果 类型 SQL 查询 获取 net | 更新日期: 2023-09-27 18:07:17

例如,下面的代码打印的是" System.Int32 ",而不是" int ":

string connArgs = "..."; // use your settings here
string query = "SELECT 1";
using (SqlConnection conn = new SqlConnection(connArgs)) {
  conn.Open();
  SqlCommand cmd = new SqlCommand(query, conn);
  using (SqlDataReader reader = cmd.ExecuteReader())
    for (int col = 0; col < reader.VisibleFieldCount; ++col)
      Console.WriteLine(reader.GetFieldType(col));
}

如何获得底层SQL类型,而不仅仅是其等效的。net系统类型?

如何在.net / c#中获取查询结果的底层SQL类型

您可以使用GetDataTypeName方法:

获取表示指定列的数据类型的字符串。

for (int col = 0; col < reader.VisibleFieldCount; ++col) {
    Console.WriteLine(reader.GetDataTypeName(col));
}