在C#中调用存储过程中的两种返回值

来源:互联网 发布:淘宝店招改全屏代码 编辑:程序博客网 时间:2024/06/08 17:39
//存储过程
   
//create proc authors_count @outrus int output
//as
//declare @authors int
//select @authors=count(*) from authors
//set @outrus=@authors
//return @authors
   System.Data.SqlClient.SqlConnection sqlcon=new System.Data.SqlClient.SqlConnection("server=(local);database=pubs;uid=sa;pwd=;");
   System.Data.SqlClient.SqlCommand sqlcmd=new System.Data.SqlClient.SqlCommand("authors_count",sqlcon);
   sqlcmd.CommandType=System.Data.CommandType.StoredProcedure;
//   sqlcmd.CommandText="authors_count";
//   sqlcmd.Connection=sqlcon;
   sqlcmd.Parameters.Add("@rus",System.Data.SqlDbType.Int);
   sqlcmd.Parameters.Add("@outrus",System.Data.SqlDbType.Int);
   sqlcmd.Parameters[0].Direction=System.Data.ParameterDirection.ReturnValue;
   sqlcmd.Parameters[1].Direction=System.Data.ParameterDirection.Output;
   sqlcon.Open();
   //int res=(int)sqlcmd.ExecuteNonQuery();//此时返回的不是存储过程的返回值,以上只是返回delete,update,insert所影响的行数
   sqlcmd.ExecuteNonQuery();
   string res=sqlcmd.Parameters[0].Value.ToString();//这样就可以得到存储过程的返回值
   sqlcon.Close();
   this.label1.Text="存储过程的返回值是:"+res.ToString();//由return 返回
   this.label2.Text="存储过程中返回的output值:"+sqlcmd.Parameters[1].Value.ToString();//由output返回