ASP.NET gets the implementation code for the stored procedure return value

  • 2020-05-16 06:53:33
  • OfStack

1. First we need to set up a table (UserInfo).
The main fields Id, uname, upass.
2. Create a stored procedure with a return value
 
create proc proc_addUserInfo 
@uname varchar(50), 
@upass varchar(50), 
@Returnc int output 
as 
begin 
insert into erp_user(u_name,u_pass)values(@uname,@upass) 
set @Returnc= isnull(@@identity,0) 
end 

The return value I get here is Id for adding records.
Background code:
 
SqlParameter[] para = new SqlParameter[] 
{ 
new SqlParameter("@uname","2012"), 
new SqlParameter("@upass","2012") 
}; 
string sql_para = "dbo.proc_addUserInfo"; 
CommandType type=CommandType.StoredProcedure; 
SQLHelper sqlh = new SQLHelper(); 
int Id=sqlh.ExecuteNoQuery(sql_para,type,para); 

SQLHelper:
 
public int ExecuteNoQuery(string sql,CommandType type,params SqlParameter [] values) 
{ 
using (SqlCommand cmd = new SqlCommand(sql, Conn)) 
{ 
cmd.CommandType = type; 
if (values != null) 
{ 
cmd.Parameters.AddRange(values); 
} 
SqlParameter Retvar = cmd.Parameters.Add("@Returnc", SqlDbType.Int); 
Retvar.Direction = ParameterDirection.Output; 
int count = cmd.ExecuteNonQuery(); 
return (int)Retvar.Value; 
} 
} 

Related articles: