存储过程输出参数的调用(简单例子)
时间:2011-03-11 来源:苑春
如:存储过程如下:
create PROC [dbo].[exchange_UserFinance]
@uId int,
@uType int,
@uAmount decimal(9,2),
@uAfterRemainAmount decimal(9,2),
@uOPid int,
@uText varchar(200),
@uNote1 varchar(200),
@uNote2 varchar(200),
@uNote3 varchar(200),
@uRemainAmount decimal(9,2) output
AS
DECLARE @uTime datetime
SET @uTime=getdate()
SET @uRemainAmount=0.00
BEGIN
BEGIN TRAN
INSERT INTO [IP_UserFinance](
[uId],[uTime],[uType],[uAmount],[uAfterRemainAmount],[uOPid],[uText],[uNote1],[uNote2],[uNote3]
)VALUES(@uId,@uTime,@uType,@uAmount,@uAfterRemainAmount,@uOPid,@uText,@uNote1,@uNote2,@uNote3)
IF @@RowCount<>1
BEGIN
ROLLBACK TRAN
SET @uRemainAmount=0
RETURN
END
UPDATE [IP_UserRemainAmount] SET [uRemainAmount] = @uAfterRemainAmount,[uTime] = @uTime WHERE uId=@uId
IF @@RowCount<>1
BEGIN
ROLLBACK TRAN
SET @uRemainAmount=0
RETURN
END
COMMIT TRAN
SET @uRemainAmount=(SELECT uRemainAmount FROM IP_UserRemainAmount WHERE uId=@uId)
END
.net调用
public static decimal modifyOfficeOrAgentFinance(IP_UserFinance agentmodel)
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter ("@uId",SqlDbType.Int ,4),
};
parms[0].Value = agentmodel.uId;
object remainagent = null;
try
{
remainagent = SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.StoredProcedure, "WSSelectIP_UserInFoFinanceOne", parms);
if (remainagent == null)
{
return -1;
}
}catch(Exception)
{
return 0;
}
SqlParameter[] parameters = {
new SqlParameter("@uId", SqlDbType.Int,4),
new SqlParameter("@uType", SqlDbType.Int),
new SqlParameter("@uAmount", SqlDbType.Decimal,9),
new SqlParameter("@uAfterRemainAmount", SqlDbType.Decimal,9),
new SqlParameter("@uOPid", SqlDbType.Int,4),
new SqlParameter("@uText", SqlDbType.VarChar,200),
new SqlParameter("@uNote1", SqlDbType.VarChar,200),
new SqlParameter("@uNote2", SqlDbType.VarChar,200),
new SqlParameter("@uNote3", SqlDbType.VarChar,200),
new SqlParameter("@uRemainAmount", SqlDbType.Decimal)};
if (Convert.ToDecimal(remainagent) < agentmodel.uAmount)
{
return -2;
}
parameters[0].Value = agentmodel.uId;
parameters[1].Value = agentmodel.uType;
parameters[2].Value = agentmodel.uAmount;
parameters[3].Value = Decimal.Add(Convert.ToDecimal(remainagent), agentmodel.uAmount);
parameters[4].Value = agentmodel.uOPid;
parameters[5].Value = agentmodel.uText;
parameters[6].Value = agentmodel.uNote1;
parameters[7].Value = agentmodel.uNote2;
parameters[8].Value = agentmodel.uNote3;
parameters[9].Direction = ParameterDirection.Output;
try
{
Convert.ToDecimal(SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.StoredProcedure, "exchange_UserFinance", parameters));
return Convert.ToDecimal(parameters[9].Value);
}
catch
{
return 0.00m;
}
}