确定查询是否在 vb.net 中返回“无行"

Determining if a query returns #39;no rows#39; in vb.net(确定查询是否在 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 中返回“无行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)