問題描述
我使用 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 中返回“無行"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!