【问题标题】:How to obtain the underlying SQL types for query results in .NET/C#?如何获取.NET/C#中查询结果的底层SQL类型?
【发布时间】:2013-03-25 02:30:38
【问题描述】:

例如,以下代码打印“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 系统类型?

【问题讨论】:

    标签: c# .net sql sql-server sql-types


    【解决方案1】:

    您可以为此使用GetDataTypeName 方法:

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

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

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 2021-05-18
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2012-07-26
      • 1970-01-01
      相关资源
      最近更新 更多