gpt4 book ai didi

c# - 尝试转换 SQL int -> c# int 时出现无效的转换异常?

转载 作者:行者123 更新时间:2023-11-30 14:30:08 25 4
gpt4 key购买 nike

<分区>

您好,我目前正在使用以下方法获取 sql server 数据库的列

public IDataReader myReader()
{
DbCommand = new SqlCommand("Select * from Mydatabase");
SqlConnection con = new SqlConnection(connectionstr);
con.Open();
DbCommand.Connection = con
return command.ExecuteReader(CommandBehavior.closeConnection);
}

IDataReader reader = myReader();
while (reader.Read())
{
int? a = (int?)reader["myIntColumn"];
}

在其他位置(对于其他表)代码工作正常并且对于此表它也工作正常,直到我来到数据库中的 int 字段。然后我得到 System.InvalidCastException(在其他表中我没有遇到这个问题)。

我做错了什么吗?还是我必须做才能完成这项工作?(有问题的 int 值在数据库中为 NULL)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com