問(wèn)題描述
我使用 MS SQL Server 作為數(shù)據(jù)庫(kù),使用 VB.NET 作為后端.
I am using MS SQL Server as my database and VB.NET as my back-end.
我想確定我在 sql 命令文本中的查詢是否不返回任何行.我試圖有一個(gè)不返回任何行的查詢,然后將其值賦予一個(gè)變?yōu)?0 的文本框.(整數(shù))
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)
現(xiàn)在的問(wèn)題是,當(dāng)我在 If 條件中測(cè)試它時(shí),它不起作用.代碼是這樣的:
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
我嘗試了一個(gè)空字符串,但它仍然不起作用.請(qǐng)幫忙!如果可能,請(qǐng)給出問(wèn)題的簡(jiǎn)單解決方案.提前致謝!
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 是對(duì)的.您將必須執(zhí)行該命令.正如我所看到的,您已經(jīng)為 Command 對(duì)象的 CommandText 屬性分配了值,該屬性是您要執(zhí)行的查詢.為了執(zhí)行命令,您必須有一個(gè)打開(kāi)的活動(dòng)連接,然后調(diào)用命令對(duì)象上的 Execute 方法.以下偽代碼可能會(huì)有所幫助:
@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
這篇關(guān)于確定查詢是否在 vb.net 中返回“無(wú)行"的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!