vb.net函数不返回sql server存储过程结果

nwlls2ji  于 2021-07-27  发布在  Java
关注(0)|答案(0)|浏览(253)

我有一个存储过程,它根据传入的参数从表中获取数据:

SELECT * 
FROM [dbo].Table1 
WHERE Column1 = @Param1 AND Column2 = @Param2

现在,我通过vb.net函数调用sql server存储过程,代码如下:

Public Function GetSPValues(ByVal Param1 As Integer,
                            ByVal Param2 As String) As DataTable
    Dim sqlCon As SqlConnection
    Dim dt As DataTable = New DataTable()
    sqlCon = strCon

    Using (sqlCon)
        Dim cmd As New SqlCommand
        cmd.Connection = sqlCon
        cmd.CommandText = "GetSP"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Param1", Param1)
        cmd.Parameters.AddWithValue("@Param2", Param2)
        sqlCon.Open()

        Dim reader As SqlDataReader = cmd.ExecuteReader()
        dt.TableName = "SPValuesTable"
        dt.Load(reader)
        Return dt
   End Using
End Function

问题是,当我尝试在SQLServerManagementStudio中执行存储过程时,它会正常工作并返回我需要的所有数据。
但是当我尝试从vb.net调用时,它根本不返回任何东西。
我不确定我传递的参数是否真的是存储过程的参数。你能帮我吗?
更新:这是我的整个sp:

ALTER PROCEDURE [dbo].[GetSP]
    @Param1 int = null,
    @Param2 nvarchar(50) = null 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT * FROM dbo.tbl1 WHERE Column1 = @Param1 AND Column2 
    = @Param2 
END

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题