嗨,我想获取输出参数的值以及select查询的结果集。
我使用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,如果没有要处理的行集)直接执行相同的查询,但是还需要执行其他几个步骤来处理返回的值。请记住,在尝试捕获Return值或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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句