嗨我想獲得輸出參數的值,以及選擇查詢的結果集。
我使用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;
可以請你告訴我如何使用它ado.net側。 – user3475314