Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的格式异常#_C#_Mysql - Fatal编程技术网

C# c语言中的格式异常#

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)

大家好

我想征求任何意见或建议

我得到了这个错误:对象不能从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"]);
但这是正确的

if (!(dtRw)["pregnant"] is DBNull)
   int pregnant = Convert.ToInt32((dtRw)["pregnant"]);
如果您使用MySqlDataReader,那么有一个IsDBNull方法可以检查列是否包含不存在或缺少的值

if(dtRw.IsDBNull("pregnant")) {
   int pregnant = Convert.ToInt32((dtRw)["pregnant"]);