0
我嘗試獲取具有搜索ID的行數。 我正在使用它來檢查重複項。返回存儲過程值
我不確定如何將計數作爲變量返回,以便我可以比較它。 我有支票If cmd.ExecuteNonQuery() = 0 Then
但它總是-1。 我試過製作Int row as Integer = cmd.ExecuteNonQuery()
,總是-1。
所以我的問題是...我做的cmd.ExecuteNonQuery()
我怎麼搶計數量的數據集的值,並將其分配給值像Dim count as Integer = ....
存儲過程後:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE UserCheck
-- Add the parameters for the stored procedure here
@ID bigint
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT COUNT(*) FROM tbl WHERE ID= @ID;
END
GO
Vb的.NET
Dim CS1 As String = ModuleGlobals.connectionString
Using con As New SqlConnection(CS1)
Dim cmd As New SqlCommand("dbo.UserCheck", con)
con.Open()
'specify that it is a stored procedure and not a normal proc
cmd.CommandType = System.Data.CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@ID", TextBox3.Text)
'Dim count As Integer = cmd.ExecuteNonQuery()
If cmd.ExecuteNonQuery() = 0 Then
Dim CS As String = ModuleGlobals.connectionString
Using con2 As New SqlConnection(CS)
Dim cmd2 As New SqlCommand("dbo.Create", con2)
con2.Open()
...add parameters here
cmd2.ExecuteNonQuery()
con2.Close()
End Using
Else
MsgBox("No user was created")
End If
con.Close()
End Using
我以前曾試過ExecuteScalar但有錯誤;不過,你的格式確實有效。不知道這就是人們使用Scalar方法的原因。感謝您提供的解釋。 – narue1992
@ narue1992:我也沒有,因爲我從來沒有真正做過任何數據庫編程:)。 - 很高興我可以幫忙!祝你好運! –