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 中返回“无行"
基础教程推荐
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01