是的,我已经看到this个问题。不,它没有解决我的问题。
我正在VB.Net中与SQL Server数据库连接的应用程序上工作。目前,我正在尝试让VB.Net应用程序在数据库上执行以下存储过程:
CREATE PROCEDURE dbo.AddSupplierAddress(
@Supplier VARCHAR(100),
@Street VARCHAR(100),
@City VARCHAR(50),
@Region VARCHAR(50),
@Code VARCHAR(7),
@Info VARCHAR(500) = NULL,
@response NVARCHAR(500) = OUTPUT
) AS
BEGIN
DECLARE @SupplierID INT, @RegionID INT
SELECT @SupplierID = supplier_id
FROM suppliers
WHERE supplier_name = @Supplier
IF @SupplierID IS NULL
BEGIN
SET @response = 'Could not add address to Supplier "' + @Supplier + '". Supplier does not exist.'
RETURN
END
SELECT @RegionID = region_id
FROM regions
WHERE region_name = @Region
IF @RegionID IS NULL
BEGIN
SET @response = 'Could not add address to Supplier "' + @Supplier + '". Region "' + @Region + '" does not exist.'
RETURN
END
IF(EXISTS(SELECT supplier_id, street FROM supplier_addresses WHERE supplier_id = @SupplierID AND street = @Street))
BEGIN
SET @response = 'Could not add address to Supplier "' + @Supplier + '". Address already exists in database.'
RETURN
END
BEGIN TRANSACTION AddSuppAddr
BEGIN TRY
INSERT supplier_addresses(supplier_id, street, city, region_id, zip_code, notes)
VALUES (@SupplierID, @Street, @City, @RegionID, @Code, @Info)
COMMIT TRANSACTION AddSuppAddr
SET @response = 'Success'
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION AddSuppAddr
DECLARE @varErrorMessage NVARCHAR(4000), @varErrorSeverity INT, @varErrorState INT;
SELECT @varErrorMessage = ERROR_MESSAGE(), @varErrorSeverity = ERROR_SEVERITY(), @varErrorState = ERROR_STATE();
RAISERROR(@varErrorMessage, @varErrorSeverity, @varErrorState);
END CATCH
END
GO
现在,我已使用以下命令在数据库本身上运行了此过程:
DECLARE @Resp NVARCHAR(500)
EXEC AddSupplierAddress 'Test Name', '123 main st.', 'Hamilton', 'Ontario', 'L84B5N', NULL, @Resp
PRINT @Resp
该程序运行正常。 (我遇到的一个小问题是,在数据库上测试这些过程时,@Resp
从未设置为我的输出值,但这是我不在此尝试解决的另一个问题)。在我的VB.Net程序中,我尝试使用以下代码运行此过程:
Private Sub CreateBtn_Click(sender As Object, e As EventArgs) Handles createBtn.Click
If CreateSupplier(supplierTxt.Text, websiteTxt.Text, True) Then
Dim message As String = ""
If setAddress Then
If AddSupplierAddress(supplierTxt.Text, streetTxt.Text, cityTxt.Text, regionTxt.Text, codeTxt.Text, infoTxt.Text, True) Then
message = "Supplier and address have been added to the database."
Else
DeleteSupplier(supplierTxt.Text)
Return
End If
Else
message = "Supplier has been added to the database."
End If
If MessageBox.Show(message & vbCrLf & "Would you like to add any Contacts?", "Data Added", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Dim contact As New NewContactForm(supplierTxt.Text, False, True) With {
.MdiParent = MdiParent
}
contact.Show()
End If
End If
End Sub
Public Function AddSupplierAddress(ByVal supplier As String,
ByVal street As String,
ByVal city As String,
ByVal region As String,
ByVal code As String,
Optional info As String = "",
Optional displayError As Boolean = False) As Boolean
Dim errorMsg As String = ""
Dim CMD As New SqlCommand("AddSupplierAddress")
CMD.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = supplier
CMD.Parameters.Add("@Street", SqlDbType.VarChar).Value = street
CMD.Parameters.Add("@City", SqlDbType.VarChar).Value = city
CMD.Parameters.Add("@Region", SqlDbType.VarChar).Value = region
CMD.Parameters.Add("@Code", SqlDbType.VarChar).Value = code
If Not info.Equals("") Then CMD.Parameters.Add("@Info", SqlDbType.VarChar).Value = info
If ExecuteCMDWithReturnValue(CMD, errorMsg) Then
Return True
Else
If displayError Then
MessageBox.Show(errorMsg, "Database Error")
Return False
Else
Return False
End If
End If
End Function
Public Function ExecuteCMDWithReturnValue(ByRef CMD As SqlCommand, ByRef errorMessage As String) As Boolean
Try
OpenDBConnection()
CMD.Parameters.Add("@response", SqlDbType.NVarChar, 500).Direction = ParameterDirection.Output
CMD.Connection = DB_CONNECTION
CMD.CommandType = CommandType.StoredProcedure
CMD.ExecuteNonQuery()
Dim result As String = CMD.Parameters("@response").Value
If result.Equals("Success") Then
ExecuteCMDWithReturnValue = True
Else
errorMessage = result
ExecuteCMDWithReturnValue = False
End If
Catch ex As Exception
Throw New Exception("Database Error: " & ex.Message)
ExecuteCMDWithReturnValue = False
Finally
CloseDBConnection()
End Try
End Function
现在,如上所述,当我在数据库上运行存储过程时,没有出现此错误。甚至更奇怪的是,我还有另一套程序被完全相同地编码,唯一的区别是supplier
的每个实例都被customer
取代,并且它们都可以正常工作!
我遇到的具体错误是标题,但您不必向上滚动它:
正式参数“ @response”未声明为OUTPUT参数,但实际参数已传递至请求的输出中。
什么可能导致我的问题?
答案 0 :(得分:4)
恭喜,您遇到了令人讨厌的T-SQL陷阱。它默认允许不带引号的情况下指定字符串参数,这样
@response NVARCHAR(500) = OUTPUT
实际上等于
@response NVARCHAR(500) = 'OUTPUT'
当然,与将参数声明为输出参数不同,因为那是
@response NVARCHAR(500) OUTPUT
这特别糟糕,因为默认值只允许使用常量,而不能使用函数或表达式,而引号是可选的,则允许使用一些确实令人误解的声明:
@from DATETIME = GETDATE
仅当在未指定@from
的值的情况下调用该过程时,此方法才会失败:此时,字符串'GETDATE'
将被转换为DATETIME
(这当然会失败)