我正在尝试运行以下代码,但遇到了转换错误。 我怎样才能重写我的代码来实现同样的目标?
boolResult= (bool?)dataReader["BOOL_FLAG"] ?? true;
intResult= (int?)dataReader["INT_VALUE"] ?? 0;
谢谢
最佳答案
在数据读取器上使用“IsDbNull”方法...例如:
bool? result = dataReader.IsDbNull(dataReader["Bool_Flag"]) ? null : (bool)dataReader["Bool_Flag"]
编辑
您需要执行类似以下操作: bool ? nullBoolean = null;
你有
bool? result = dataReader.IsDbNull(dataReader["Bool_Flag"]) ? nullBoolean : (bool)dataReader["Bool_Flag"]
关于c# - 将 datareader 值转换为可空变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5409936/