2013-03-04 18 views
0

我一直對此感到頭痛。MS SQL查詢參數已通過,但在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 

什麼我做錯了任何幫助嗎?

我在使用存儲過程方面相當新,但我仍然無法找到問題。

謝謝

+1

我懷疑,這是原來的代碼,因爲你必須設置'SqlCommand's' ['CommandType'(HTTP: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtype(v=vs.110).aspx)屬性到['StoredProcedure'](http://msdn.microsoft.com /en-us/library/system.data.commandtype.aspx)。 – 2013-03-04 22:18:19

+0

@TimSchmelter,這正是問題所在。我在代碼中的每一個存儲過程調用中都有一行,並且錯過了一次。謝謝! – Blunderfest 2013-03-04 22:57:27

回答

1

你必須設置SqlCommand'sCommandType屬性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 
0

如果字符串參數,@subWeek,爲空就可以得到這個錯誤。

如果您的字符串爲空,則需要爲@subWeek傳遞DBNull.Value。

嘗試硬編碼@subWeek值

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

這應該很好地工作。