从存储过程中获取值

时间:2014-05-23 07:33:40

标签: vb.net stored-procedures

我有这段代码:

 Dim parameterlist(1) As BaseClasses.Data.StoredProcedureParameter
 Dim selects As BaseClasses.Data.StoredProcedureParameter = New BaseClasses.Data.StoredProcedureParameter("@p_where_strempid", "empcode=602256", System.Data.SqlDbType.Text, System.Data.ParameterDirection.Input)
 Dim wheres As BaseClasses.Data.StoredProcedureParameter = New BaseClasses.Data.StoredProcedureParameter("@p_where_str2", "applicableyear=2014 and (applicablemonth between 1 and 2)", System.Data.SqlDbType.Text, System.Data.ParameterDirection.Input)

parameterlist(0) = selects
parameterlist(1) = wheres

Dim test As BaseClasses.Data.StoredProcedure = New BaseClasses.Data.StoredProcedure("DatabasePAYNORTH1", "pMyApp1Tbl_myproced", parameterlist)


            If (test.RunNonQuery()) Then


                Dim outputParameter As System.Data.IDataParameter


                For Each outputParameter In test.OutputParameters


                    Dim outputValue As Object = outputParameter.Value

                    MsgBox(outputValue.ToString)


                Next


            Else

我的错误是什么?当我运行代码我每个都有错误..这是什么错误?我只是想在存储过程中获取我的记录。希望你能帮助我们。感谢

编辑..

这是我在sql server中的存储过程


    ALTER PROCEDURE [dbo].[pMyApp1Tbl_myproced]
            @p_where_strempid nvarchar(4000),
            @p_where_str2 nvarchar(4000)
    AS 
    DECLARE
        @l_query_select nvarchar(4000),
        @l_query_from nvarchar(4000),
        @l_end varchar(4000)
    BEGIN
        SET NOCOUNT ON

            -- If page number and batch size are not valid numbers return the empty result set
            SET @l_query_select = 'SELECT  top 6 net from(select a.empcode,sum(a.netincome)as net,b.applicableyear,b.applicablemonth
                                   from  [tbl_fpPayrollDetails] a inner join tbl_fppayroll b on a.payrollno = b.payrollno where '
            SET @l_query_from=' group by empcode,applicableyear,applicablemonth ) as src where '
            SET @l_end =' order by applicablemonth , applicableyear '

            EXEC(@l_query_select + @p_where_strempid + @l_query_from + @p_where_str2 + @l_end);


        SET NOCOUNT OFF



    END

1 个答案:

答案 0 :(得分:0)

如果您有一个带输出参数的程序

CREATE PROCEDURE [dbo].[pMyApp1Tbl_myproced]
        @p_where_strempid nvarchar(4000),
        @p_where_str2 nvarchar(4000),
        @OutputParameterName int OUTPUT
AS 

然后通过输出参数获取值,您需要声明它以及输入参数。

Dim p As BaseClasses.Data.StoredProcedureParameter 
p = New BaseClasses.Data.StoredProcedureParameter("@OutputParameterName", Nothing, System.Data.SqlDbType.Int, System.Data.ParameterDirection.Output)    
'NOTE: if this parameter has a string type such as varchar, nvarchar, string, etc. you have to set its size:
'p.Size = 10

Dim parameterList(2) As BaseClasses.Data.StoredProcedureParameter
...
parameterlist(0) = selects
parameterlist(1) = wheres    
parameterlist(2) = p

在返回一组记录时使用RunQuery。通过输出参数返回一个或多个值时使用RunNonQuery。

因此,如果您没有输出参数,并且需要浏览数据集并访问每个行和列,请使用

If test.RunQuery() Then
    Dim ds As System.Data.DataSet = test.DataSet
    Dim dt As System.Data.DataTable = ds.Tables.Item(0)
    Dim r As System.Data.DataRow
    For Each r In dt.Rows   
        Dim s as String = myRow.Item(0).ToString()  
    Next
End If