请教如何获得存储过程返回值
最近转用C#,很多地方不熟悉,昨天感谢两位同学,好人啦!
存储过程内容如下
ALTER PROCEDURE Tc_addorder
(
@ordercode varchar(20),
@userID INT,
@ID INT OUTPUT
)
as
--DECLARE @ID int,@cmaker varchar(20)
DECLARE @cmaker varchar(20)
set @ID = (select max(ID) from pre_somain)
set @cmaker = (select name from sys_user where id = @userID)
if @ID is null or @ID = 0
SET @ID = 1
else
set @ID = @ID + 1
--INSERT pre_somain INTO
INSERT [pre_somain]
(ID,csocode,cmaker,ddate)
VALUES
(@ID,@ordercode,@cmaker,GETDATE())
RETURN @ID
现在在C#中调用,要获得返回值
cmd.Parameters[0].Value = ordercode;
cmd.Parameters[1].Value = userID;
cmd.Parameters.Add("@ID",SqlDbType.Int,4);
cmd.Parameters[2].Direction = ParameterDirection.Output;
int result = -1;
try
{ ///打开连接
con.Open();
///操作数据
result = cmd.ExecuteNonQuery();
}
catch(Exception ex)
{ ///抛出异常
throw new Exception(ex.Message,ex);
}
finally
{ ///关闭连接
con.Close();
}
return (int)ParameterDirection.Output;
但是上面的代码获取的返回结果不对,不是ID值,有时是6,有时是2,似乎是影响的行数值。
------解决方案--------------------
spTransactRegisterPerson[15] = new SqlParameter("@PersonId", SqlDbType.Int);
spTransactRegisterPerson[15].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(SqlHelper.connectionStringQqtb, CommandType.StoredProcedure, "RegisterPerson", spTransactRegisterPerson);
return Convert.ToInt32(spTransactRegisterPerson[15].Value);
执行完了你的存储过程直接读参数value即可