如何在.NET/C#中获取查询结果的底层SQL类型?

3
例如,以下代码会打印出 "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系统中对应的类型?

1个回答

5
你可以使用GetDataTypeName方法来实现:

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接