vb.net 如何检查 DataReader 是否有数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1812254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:32:24 来源:igfitidea点击:
How Can I check whether DataReader has Data or not?
提问by RedsDevils
Again I have problem with checking whether DataReader object has data or not?
我再次在检查 DataReader 对象是否有数据时遇到问题?
Dim cmd as SqlCommand
Dim drd as SqlDataReader
cmd = New SqlCommand ("SELECT * FROM Stock", conx)
drd = cmd.ExecuteReader()
''HERE I WOULD LIKE TO CHECK WHETHER drd has Data or not
While (drd.Read())
{
txtName.Text = drd.Item("StockName")
}
How can I check that? Please Help me! Thanks all in advcance!
我该如何检查?请帮我!先谢谢大家!
回答by richeym
if(drd.HasRows)
{
//....
}
回答by Sensa
Yes you can with drd.read()
是的,您可以使用 drd.read()
Like:
喜欢:
If drd.read() Then
...do things with data...
Else
...show message box... or just skip.
End If
回答by ZippyV
drd.Read() will return False when there is no data. You don't have to change your code.
drd.Read() 将在没有数据时返回 False。您不必更改代码。