WEB开发网
开发学院WEB开发ASP.NET ado.net获取存储过程的返回值 阅读

ado.net获取存储过程的返回值

 2005-06-26 17:49:25 来源:WEB开发网   
核心提示:假设有存储过程如下:-CREATE PRoc sp_uptmp @tmpName varchar(50),@srcPos varchar(255)asBegin TRAN insert into t_template values(@tmpName,@srcPos)COMMIT return isnull(@@ide

--------------------------------------------
假设有存储过程如下:
---------------------------------------------

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;  //此处即为返回值

Tags:ado net 获取

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接