它是VS2008中的一個ASP.net應用程序,連接到SQL 2005數據庫。SQLClient輸出參數返回DBNull
沒有錯誤調用存儲過程,db更新成功,但OUTPUT參數始終返回DBnull。下面VB代碼:
Dim ConnectString As String = "", connect As New Data.SqlClient.SqlConnection
ConnectString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
connect.ConnectionString = ConnectString
Dim cmd As New SqlCommand("saveAccess", connect)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@Name", "SampleName"))
Dim outparam As SqlParameter = New SqlParameter("@returnValue", SqlDbType.Int)
outparam.Direction = ParameterDirection.Output
cmd.Parameters.Add(outparam)
connect.Open()
cmd.ExecuteNonQuery()
If IsDBNull(cmd.Parameters("@returnValue").Value Then
Response.Write("Why does it always returns DBNull")
Else : Response.Write(cmd.Parameters("@returnValue").Value.ToString())
End If
connect.Close()
下面是SQL代碼
ALTER PROCEDURE [dbo].[saveAccess]
(@Name NVARCHAR(20), @returnValue INT OUTPUT)
AS
BEGIN
INSERT INTO Access ([Name]) VALUES (@Name);
SELECT @returnValue = @@ROWCOUNT;
END
不知道那是什麼我做了愚蠢的錯誤。任何輸入都有幫助
感謝
你需要關閉連接,才能讀取輸出參數,在存儲過程被設置。輸出參數在連接關閉後在變量中設置。 –
解決方案(就像我自己說的那樣愚蠢):錯過了returnValue變量前面的@符號。我在本文中正確地輸入了代碼,但是我沒有在SP中使用@。 'code'wrong:SELECT returnValue = @@ ROWCOUNT; 'code'correct:SELECT @returnValue = @@ ROWCOUNT; – user836107