2014-03-29 69 views
1

嗨我想獲得輸出參數的值,以及選擇查詢的結果集。
我使用ExecuteNonQuery,它給出了適當的輸出參數值。
我使用ExecuteReader它沒有給出適當的輸出參數值,但它爲選擇查詢給出適當的值。
那麼我應該用什麼來獲得兩個結果。如何在ado.net中使用輸出參數並選擇sql server存儲過程的查詢結果?

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; 

回答

1

此相同的查詢,可直接與Command.ExecuteReader卻執行(或的ExecuteNonQuery,如果你沒有一個行集處理), 但也有你需要採取一些其他步驟處理返回的值。請記住,在嘗試捕獲返回值或 OUTPUT參數之前,您必須完成對所有行集的處理。以下代碼顯示瞭如何使用ExecuteReader和一個循環來處理行集,然後捕獲返回值和參數輸出 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 
0

一種可能的方式是在代碼在所有

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 

使用ADO的NextRecordSet而不是使用輸出參數

得到的第一個記錄集這是你的參數 然後rs = rs.NextRecordSet() ...讀你的行

性能:輸出參數vs select真的不在這裏考慮

+0

可以請你告訴我如何使用它ado.net側。 – user3475314

相關問題