Determining if a query returns #39;no rows#39; in vb.net(确定查询是否在 vb.net 中返回“无行)
问题描述
我使用 MS SQL Server 作为数据库,使用 VB.NET 作为后端.
I am using MS SQL Server as my database and VB.NET as my back-end.
我想确定我在 sql 命令文本中的查询是否不返回任何行.我试图有一个不返回任何行的查询,然后将其值赋予一个变为 0 的文本框.(整数)
I want to determine if my query in sql command text returns no rows. I tried to have a query that returns no rows, and then give its value to a text box which became 0. (integer)
现在的问题是,当我在 If 条件中测试它时,它不起作用.代码是这样的:
The problem now, is that when I test it in an If condition, it doesn't work. The code is like this:
If (Query that returns no rows) = 0 Then
'condition
Else
'condition
End If
我尝试了一个空字符串,但它仍然不起作用.请帮忙!如果可能,请给出问题的简单解决方案.提前致谢!
I tried a blank string, but it still does not work. Please help! If possible, please give the simples solution to the problem. Thank you in advance!
推荐答案
@podiluska 是对的.您将必须执行该命令.正如我所看到的,您已经为 Command 对象的 CommandText 属性分配了值,该属性是您要执行的查询.为了执行命令,您必须有一个打开的活动连接,然后调用命令对象上的 Execute 方法.以下伪代码可能会有所帮助:
@podiluska is right. You will have to execute the command. As I can see you have assigned value for the CommandText property of Command object that’s the query you want to execute. In order to execute the command, you must have an open active connection followed by call to Execute method on command object. Following pseudo code might help:
Public Sub Temp(ByVal connectionString As String)
Dim queryString As String = _
" select * from example where id = 999;"
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
Try
If reader.Read() Then
'If condition
Else
'condition with no results
End If
Finally
reader.Close()
End Try
End Using
End Sub
这篇关于确定查询是否在 vb.net 中返回“无行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:确定查询是否在 vb.net 中返回“无行"


基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01