MS SQL查询参数已传递,但在VB.net中无法识别

时间:2013-03-04 22:15:21

标签: asp.net vb.net visual-studio-2010 sql-server-2008

我现在一直在反对这个问题。

我有一个从表中检索字段并从中填充输入的函数,但是当我尝试执行该函数时,我收到一条错误,说“过程或函数”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

对我做错的任何帮助?

我在使用存储过程时相当新,但我仍然找不到问题。

谢谢

2 个答案:

答案 0 :(得分:1)

您必须将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

答案 1 :(得分:0)

如果您的字符串参数@subWeek为null,则可能会出现此错误。

如果您的字符串为null,则需要为@subWeek传递DBNull.Value。

尝试对@subWeek值进行硬编码

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

这应该可以正常工作。