Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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
使用reader HasRows的VB.net应用程序_Vb.net - Fatal编程技术网

使用reader HasRows的VB.net应用程序

使用reader HasRows的VB.net应用程序,vb.net,Vb.net,我正在用vb.net中的数据读取器运行SQL查询 reader3 = myCommand3.ExecuteReader reader3.Read() If reader3.HasRows Then FromToDates = reader3.GetString(3) End If 在此特定查询中没有返回任何行,但是即使正在使用ifreader3.HasRows,它仍然显示一个错误,说明: Additional information: Data is Null. This method

我正在用vb.net中的数据读取器运行SQL查询

reader3 = myCommand3.ExecuteReader
reader3.Read()
If reader3.HasRows Then
    FromToDates = reader3.GetString(3)
End If
在此特定查询中没有返回任何行,但是即使正在使用if
reader3.HasRows
,它仍然显示一个错误,说明:

Additional information: Data is Null. This method or property cannot be called on Null values.

这是在尝试设置
FromToDates
变量时发生的,但是没有返回任何行,因此它甚至不应该到达此部分

如果您使用的是Reader.read(),那么我认为您不需要Reader,hasrow()。你可以做这样的事

reader3 = myCommand3.ExecuteReader
If reader3.Read()Then
FromToDates = reader3.GetString(3)
End If

与您的错误相关的是,看起来您正在从数据库中获取NULL值,并且在vb中无法读取NULL,我建议在SQL脚本中使用ISNULL(Column_NAME“”)函数。

我怀疑是否没有行,我假设至少有一行的值为
NULL
。您可以通过以下方式进行检查:

但是,变量名表明它是一个日期,但您将其存储为字符串。使用正确的类型,即
date
datetime
。如果它实际上是一个
日期时间
使用:

Dim FromToDates As Date 
If reader3.HasRows AndAlso Not reader3.IsDbNull(3)  Then
    FromToDates = reader3.GetDateTime(3)
End If

看起来,即使行是空的(可能全部为DBNull),也有一个从查询返回的行对象。本线程中提供了几种不同的解决方案,它们都采用不同的方法在空行出错之前捕获空行


我使用Dim FromToDates作为DateTime,因此将此变量设置为DateTimevariable@Charles:然后您应该将
选项Strict
设置为
On
并首先学会使用正确的类型;)@查尔斯:我已经编辑了我的答案,使用
reader.GetDateTime
Date
变量。仍然得到相同的错误,我想看看是否返回了行。我不想查看列是否为null(DBNull)@Charles:您可以使用
HasRows
检查是否有行,并使用
IsDbNull
检查值是否为null。
Dim FromToDates As Date 
If reader3.HasRows AndAlso Not reader3.IsDbNull(3)  Then
    FromToDates = reader3.GetDateTime(3)
End If