确定查询是否在 vb.net 中返回“无行" [英] Determining if a query returns 'no rows' in vb.net

查看:26
本文介绍了确定查询是否在 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 中返回“无行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆