C# c语言中的格式异常#
大家好 我想征求任何意见或建议 我得到了这个错误:对象不能从DBNull转换为其他类型,但它访问或引用的字段没有null值。怎么可能呢 下面是一段代码:C# c语言中的格式异常#,c#,mysql,C#,Mysql,大家好 我想征求任何意见或建议 我得到了这个错误:对象不能从DBNull转换为其他类型,但它访问或引用的字段没有null值。怎么可能呢 下面是一段代码: int pregnant = Convert.ToInt32((dtRw)["pregnant"]); 非常感谢。DBNull和null是不同的。虽然null不是任何类型的实例,但System.DbNull.Value是System.DbNull的实例。阅读更多 以下代码将失败 if ((dtRw)["pregnant"] != null)
int pregnant = Convert.ToInt32((dtRw)["pregnant"]);
非常感谢。DBNull和null是不同的。虽然null不是任何类型的实例,但System.DbNull.Value是System.DbNull的实例。阅读更多 以下代码将失败
if ((dtRw)["pregnant"] != null)
int pregnant = Convert.ToInt32((dtRw)["pregnant"]);
但这是正确的
if (!(dtRw)["pregnant"] is DBNull)
int pregnant = Convert.ToInt32((dtRw)["pregnant"]);
如果您使用MySqlDataReader,那么有一个IsDBNull方法可以检查列是否包含不存在或缺少的值
if(dtRw.IsDBNull("pregnant")) {
int pregnant = Convert.ToInt32((dtRw)["pregnant"]);