簡體   English   中英

已傳遞MS SQL查詢參數,但在VB.net中無法識別

[英]MS SQL query parameter passed, but not recognized in VB.net

我已經為此努力了一段時間。

我有一個從表中檢索字段並從中填充輸入的函數,但是,當我嘗試執行該函數時,收到一條錯誤消息:“過程或函數'Quad_GetAllFields'需要參數'@subWeek',但未提供”。

我知道我正在提供參數,因為我可以從函數的任何位置訪問它。

該函數是:

Protected Sub setAllFields(ByVal myConnection As SqlConnection, ByVal subWeek As String, ByVal site As String)
    Dim myCommand = New SqlCommand("Quad_GetAllFields", myConnection)
    myCommand.Parameters.AddWithValue("@subWeek", subWeek)
    myCommand.Parameters.AddWithValue("@site", site)
    Dim myDataReader As SqlDataReader = myCommand.ExecuteReader()
    If myDataReader.Read() Then
        'populate fields
    End If
End Sub

存儲過程為:

ALTER PROCEDURE dbo.Quad_GetAllFields
(
@subWeek VARCHAR(50),
@site VARCHAR(20)   
)
AS
BEGIN
SELECT TOP (1) ID, Priority1, Priority2, Priority3, Priority4, Highlight1, Highlight2, Highlight3, Highlight4, Update1, Update2, Update3, Ahead1, Ahead2, Ahead3, Outlook1, Outlook2, Comments, Site, Week, Submitted FROM Charts WHERE Week = @subWeek AND Site = @site ORDER BY ID DESC
END

對我做錯了什么有幫助嗎?

我在使用存儲過程方面還很陌生,但是仍然找不到問題。

謝謝

您必須將SqlCommand's CommandType屬性設置為StoredProcedure ,默認為Text

Protected Sub setAllFields(ByVal myConnection As SqlConnection, ByVal subWeek As String, ByVal site As String)
    Dim myCommand = New SqlCommand("Quad_GetAllFields", myConnection)
    myCommand.CommandType = CommandType.StoredProcedure
    myCommand.Parameters.AddWithValue("@subWeek", subWeek)
    myCommand.Parameters.AddWithValue("@site", site)
    Dim myDataReader As SqlDataReader = myCommand.ExecuteReader()
    If myDataReader.Read() Then
        'populate fields'
    End If
End Sub

如果您的字符串參數@subWeek為空,則會出現此錯誤。

如果字符串為null,則需要為@subWeek傳遞DBNull.Value。

嘗試對@subWeek值進行硬編碼

myCommand.Parameters.AddWithValue("@subWeek", "Myvalue")

這應該工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM