簡體   English   中英

如何在ado.net中使用輸出參數並選擇sql server存儲過程的查詢結果?

[英]How to use output parameter and select query result of a sql server stored procedure in ado.net?

嗨,我想獲取輸出參數的值以及選擇查詢的結果集。
我使用了ExecuteNonQuery ,它為輸出參數提供了適當的值。
我使用ExecuteReader它沒有為輸出參數提供適當的值,但它為選擇查詢提供了適當的值。
那么我應該用什么來獲得這兩個結果。

    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;

可以使用Command.ExecuteReader (或ExecuteNonQuery,如果您沒有要處理的行集)直接執行相同的查詢,但是您還需要執行其他幾個步驟來處理返回的值。 attempting to capture the Return value or parameters.請記住,嘗試捕獲返回值或參數,您必須完成對所有行集的處理。 parameters.以下代碼顯示如何使用 ExecuteReader 和循環來處理行集,然后捕獲返回值和參數。 parameters (even a lot of them) can be handled far faster than even a single row of data returned by a SELECT .您會發現參數(甚至很多)的處理速度甚至比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

一種可能的方法是使用 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真的不是這里的考慮因素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM