--------------------------------------------
假設有存儲過程如下:
---------------------------------------------
CREATE proc sp_uptmp @tmpName varchar(50),@srcPos varchar(255)
as
Begin TRAN
insert into t_template values(@tmpName,@srcPos)
COMMIT
return isnull(@@identity,0)
GO
------------------------------------------------------------
在 ado.Net 裡面獲取返回值的方法為(c#):
------------------------------------------------------------
SqlConnection dbconn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("sp_uptmp",dbconn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tmpName = cmd.Parameters.Add("@tmpName",SqlDbType.VarChar);
SqlParameter srcPos = _cmd.Parameters.Add("@srcPos",SqlDbType.VarChar);
SqlParameter rtnval = cmd.Parameters.Add("rval",SqlDbType.Int);
tmpName.Direction = ParameterDirection.Input;
srcPos.Direction = ParameterDirection.Input;
rtnval.Direction = ParameterDirection.ReturnValue;
tmpName.Value = "";
srcPos.Value = "";
dbconn.Open();
cmd.ExecuteNonQuery();
dbconn.Close();
tmpid = (int)rtnval.Value; //此處即為返回值