为什么 SqlDataReader 似乎修改了它的数据类型?

Dom*_*que 1 c# sql-server sqldatareader

这个问题是这个DBA相关问题之后的下一步

我现在有以下 C# 代码,基于变量SqlCommand sqlCommandSqlDataReader sqlDataReader.

我正在运行这段代码:

// What's the max difference of two "Own_Table" tupples for "Field_Name" column?
sqlCommand.CommandText = $"SELECT MAX(val - lag_value) FROM " + 
                         $" (SELECT t.{Field_Name} AS val,LAG(t.{Field_Name}) " +
                         $"    OVER(ORDER BY t.{Field_Name}) as lag_value " +
                         $"  FROM {Own_Table} t) AS tmp " +
                         $"WHERE lag_value IS NOT NULL";
sqlCommand.Parameters.Clear();
sqlDataReader = sqlCommand.ExecuteReader();
long max_value = 0;
while (sqlDataReader.Read())
{
    max_value = sqlDataReader.GetInt32(0); <== sometimes NOK
}
Run Code Online (Sandbox Code Playgroud)

对于一个特定的领域,这似乎出错了:对于该领域Id,提到的源代码行生成了一个System.InvalidCastException,提到Specific cast is not valid..

这是正确的,因为在即时窗口中,我可以看到以下结果:

? sqlDataReader.GetFieldType(0).ToString()
"System.Int64"
Run Code Online (Sandbox Code Playgroud)

但是,DataType在相应的 DataTable 列中的 似乎是System.Int32,正如您可以从以下直接窗口的摘录中看到的(dt_mainDataTablei表示相应列的索引):

? dt_main.Columns[i].DataType
{Name = "Int32" FullName = "System.Int32"}
Run Code Online (Sandbox Code Playgroud)

在具有相同 的前一列中DataType,一切正常。

所以现在我的问题是:System.Int32列上的查询结果怎么可能突然决定将其System.Int64作为数据类型,以及处理这个问题的最佳方法是什么?(我已经尝试全部替换sqlDataReader.GetInt32()sqlDataReader.GetInt64()但这也失败了)。

DataTable dt_main的结构是从检索INFORMATION_SCHEMA.COLUMNS如下:

sqlCommand.CommandText = 
  "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " + 
  "WHERE TABLE_NAME = @selectedItem";
sqlCommand.Parameters.Clear();
sqlCommand.Parameters.AddWithValue("selectedItem", cmb_Table_Names.SelectedItem);
// cmb_Table_Names contains all table names.
sqlDataReader = sqlCommand.ExecuteReader();

while (sqlDataReader.Read()) // Get the names of all the columns
{
    string tmp_ColName = sqlDataReader.GetString(0);
    string tmp_ColDataType = sqlDataReader.GetString(1);
    Type T = typeof(string);
    switch (tmp_ColDataType)
    { 
        case "int": case "bigint": case "tinyint": case "smallint":
            T = typeof(int);
            break;
        case "bit":
            T = typeof(bool);
            break;
        case "varchar": case "datetime": case "text":
            T = typeof(string);
            break;
        default: 
            T = typeof(string);
            break;
    }
    dt_main.Columns.Add(tmp_ColName,T);
Run Code Online (Sandbox Code Playgroud)

Hei*_*nzi 5

您分析架构信息的代码已损坏。这里:

    case "int": case "bigint": case "tinyint": case "smallint":
        T = typeof(int);
Run Code Online (Sandbox Code Playgroud)

你认为在SQL Server类型intbiginttinyintsmallint所有映射到C#类型int(即.NET Framework类型Int32)。

这是不正确的。bigint例如,对应于 C# 的long,即 .NET Frameworks 的Int64.

您可以使用以下 MSDN 条目来查找正确的映射: