如何在ado.net中使用输出参数并选择sql server存储过程的查询结果?

时间:2014-03-29 08:30:58

标签: c# sql-server-2008 stored-procedures ado.net

您好我想获取输出参数的值以及select查询的结果集。
我使用ExecuteNonQuery,它为输出参数提供了适当的值。
我使用ExecuteReader它没有为输出参数提供适当的值,但它为select查询提供了适当的值。
那么我应该用什么来获得结果。

    ALTER PROCEDURE [dbo].[XYZ] 
(  
 @szUserName varchar(50),  
 @iOutDistinceBankCount int out
)  
AS  
BEGIN  
declare @iCountDistinctBanks int;
set @iCountDistinctBanks = (select count (distinct a.DCC_BANK_ID )
 from DEF a with(nolock)
join ABC b with(nolock) on
 a.ROLEID = b.ROLEID
where b.USERNAME = @szUserName and b.STATUS_ID = 2)

if ((@iCountDistinctBanks > 1) or (@iCountDistinctBanks = 0))
    begin
        set @iOutDistinceBankCount = @iCountDistinctBanks 
    end

else
    begin
        set @iOutDistinceBankCount = 1;
            select a.DCC_BANK_ID as DCC_BANK_ID
            from DEF a with(nolock)
            join ABC b with(nolock) on
            a.ROLEID = b.ROLEID
            where b.USERNAME = @szUserName and b.STATUS_ID = 2
    end

END 

这是我的C#代码。

Int32 i32DistinctDCCBankCount = -1;
            Int64 i64BankStaticID = -1;
            InitDB();
            m_command = new SqlCommand("DCC_spUIDCCBankIdAccordingUser", m_con);
            m_command.Parameters.Add("@szUserName", System.Data.SqlDbType.VarChar, 50).Value = MerchantName;

            SqlParameter output = new SqlParameter("@iOutDistinceBankCount", System.Data.SqlDbType.Int);
            output.Direction = System.Data.ParameterDirection.Output;
            m_command.Parameters.Add(output);

            m_command.CommandType = System.Data.CommandType.StoredProcedure;
            m_con.Open();
           // m_reader = m_command.ExecuteReader();
            m_command.ExecuteNonQuery();
            i32DistinctDCCBankCount = Convert.ToInt32(m_command.Parameters["@iOutDistinceBankCount"].Value);


                    if (i32DistinctDCCBankCount == 0)
                    {
                        iDistinctDCCBankCount = 0;
                        return i32DistinctDCCBankCount;
                    }
                    else if (i32DistinctDCCBankCount > 1)
                    {
                        iDistinctDCCBankCount = i32DistinctDCCBankCount;
                        return -2;
                    }
                    else if (i32DistinctDCCBankCount == 1)
                    {
                        i64BankStaticID = Convert.ToInt64(m_reader["DCC_BANK_ID"]);
                        iDistinctDCCBankCount = i32DistinctDCCBankCount;
                        return i64BankStaticID;
                    }


            iDistinctDCCBankCount = 0;
            return 0;

2 个答案:

答案 0 :(得分:2)

可以使用 Command.ExecuteReader 直接执行相同的查询(如果没有要处理的行集,则 ExecuteNonQuery ),  但是,您需要采取其他几个步骤来处理返回的值。请记住,在尝试捕获返回值或 OUTPUT 参数之前,您必须完成所有行集的处理。以下代码显示如何使用ExecuteReader和循环处理行集,然后捕获Return值和 OUTPUT 参数。您会发现 OUTPUT 参数(甚至很多参数)的处理速度远远快于 SELECT 返回的单行数据。

这是示例

    With cmd.Parameters
        cn.Open()
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        ' Process rowset(s)
        bolEOF = dr.Read
        Do
            Do While bolEOF = True
                ' Process rows
                bolEOF = dr.Read()
            Loop
        Loop While dr.NextResult = True
        cmd.Cancel()
// you need to close dataReader first
        dr.Close()


        Debug.WriteLine("@iOutDistinceBankCount:" & _
            .Item("@iOutDistinceBankCount").Value.ToString)
    End With

答案 1 :(得分:0)

一种可能的方法是使用ado的NextRecordSet并且根本不使用输出参数

if ((@iCountDistinctBanks > 1) or (@iCountDistinctBanks = 0))
    begin
        set @iOutDistinceBankCount = @iCountDistinctBanks 
        select @iOutDistinceBankCount as OutDistinceBankCount 
        select 0 where 0 = 1
    end

else
    begin
        set @iOutDistinceBankCount = 1;
        select @iOutDistinceBankCount as OutDistinceBankCount 

            select a.DCC_BANK_ID as DCC_BANK_ID
            from DEF a with(nolock)
            join ABC b with(nolock) on
            a.ROLEID = b.ROLEID
            where b.USERNAME = @szUserName and b.STATUS_ID = 2
    end

END 

代码

获取第一个记录集,这是您的参数 然后rs = rs.NextRecordSet() ...读取你的行

性能:输出参数vs select实际上不是这里的考虑因素