C#调用存储过程并使用output和returnvalue值---总结C#调用存储过程并使用output和returnvalue值---总结1. MSSQL 存储过程有 input,output,returnvalue 等类型参数。 其中input和output参数必须声明,returnvalue不需要在SQL语句声明 2. Asp.net 页面中调用 存储过程,指定commadType=stroedProucedure; 指定参数 例子; SqlParameter parm=new SqlParameter(); parm.ParameterName="@name"; parm.SqlDbType=SqlDbType.VarChar; parm.Direction=ParameterDirection.Input; //output,returnvalue等 parm.Value=nameValue; 获取存储过程返回的数据集和 output,returnvalue值 DataSet ds=new DataSet(); da.Fill(ds); Identity = cmd.Parameters["@Identity"].Value.ToString(); RowCount = cmd.Parameters["@ROWCOUNT"].Value.ToString(); return ds;
1.创建 带input 参数的 简单存储过程
创建存储过程 CREATE PROCEDURE创建存储过程 CREATE PROCEDURE--//存储过程 USE mydata Go CREATE PROCEDURE [wowo] AS SELECT * FROM [wowomiao] go
修改存储过程,执行存储过程修改存储过程,执行存储过程ALTER PROCEDURE [wowo] @name varchar(100) --声明变量 AS DECLARE @sql varchar(200) SET @sql='select * from [wowomiao]' --区分大小写sql SQL if @name!='' --if begin .. end begin SET @sql=@sql+'WHERE name like ''%'+@name+'%''' --变量拼接++,'单引号,特殊字符用单引号 END exec(@sql) --exec(@sql) print @sql go exec [wowo] @name='2' --执行存储过程
2.创建 input,output,returnvalue 参数的 简单存储过程
修改存储过程添加 output参数,returnvalue参数ALTER PROCEDURE [wowo] @name varchar(100) ,@Identity int OUTPUT AS DECLARE @sql varchar(200) SET @sql='select * from [wo]' --区分大小写sql SQL if @name!='' begin SET @sql=@sql+'WHERE name like ''%'+@name+'%''' --变量拼接++,'单引号,特殊字符用单引号 END exec(@sql) --exec(@sql) print @sql SET @Identity =SCOPE_IDENTITY() print @Identity print @@ROWCOUNT RETURN @@ROWCOUNT go exec [wowo] @name='',@identity=0
default.cs(Gridview数据绑定)protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["name"] == null || Request.Cookies["name"].Value == "") { Response.Redirect("Login.aspx?returnUrl="+Request.Url); } else { //if (!IsPostBack) //{ //DataSet ds = sql.getdataset("select * from wowotuan_miaosha"); string Identity=null; string rowcount=null; DataSet ds = sql.DataSetProcedure("wowo", "2",ref Identity,ref rowcount); lbTest.Text = "Identity="+Identity+"<br/>rowcount="+rowcount; GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); //} }
sql.csusing System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.ComponentModel; using System.Data.SqlClient; using System.Data; namespace _55tuan { public class sql { private static string connection = "server=(local);Database =mydata;Integrated Security=true;"; public static DataSet getdataset(string commd) { using (SqlConnection con = new SqlConnection(connection)) { con.Open(); SqlCommand cmd = new SqlCommand(commd, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } /// <summary> /// C# 类方法获取存储过程返回的数据和返回值 /// </summary> /// <param name="ProucedureName">存储过程名称</param> /// <param name="nameValue">参数name</param> /// <param name="Identity">output参数identity</param> /// <param name="RowCount">returnvalue参数rowcount</param> /// <returns></returns> public static DataSet DataSetProcedure(string ProucedureName, string nameValue, ref string Identity,ref string RowCount) { using (SqlConnection con=new SqlConnection(connection)) { using(SqlCommand cmd=new SqlCommand()) { cmd.CommandType=CommandType.StoredProcedure; cmd.CommandText=ProucedureName; cmd.Connection=con; SqlParameter parm=new SqlParameter(); parm.ParameterName="@name"; parm.SqlDbType=SqlDbType.VarChar; parm.Direction=ParameterDirection.Input; parm.Value=nameValue; //声明output参数 Identity SqlParameter parm1=new SqlParameter(); parm1.ParameterName = "@Identity"; parm1.Direction = ParameterDirection.Output; parm1.SqlDbType = SqlDbType.VarChar; parm1.Value = ""; //声明returnvalue参数 ROWCOUNT SqlParameter parm2 = new SqlParameter(); parm2.ParameterName = "@ROWCOUNT"; parm2.Direction = ParameterDirection.ReturnValue; parm2.SqlDbType = SqlDbType.VarChar; parm2.Value = ""; cmd.Parameters.Add(parm); cmd.Parameters.Add(parm1); cmd.Parameters.Add(parm2); con.Open(); using(SqlDataAdapter da=new SqlDataAdapter(cmd)) { //Identity =cmd.Parameters["@Identity"].Value.ToString(); //RowCount = cmd.Parameters["@ROWCOUNT"].Value.ToString(); DataSet ds=new DataSet(); da.Fill(ds); Identity = cmd.Parameters["@Identity"].Value.ToString(); //获取output参数 RowCount = cmd.Parameters["@ROWCOUNT"].Value.ToString(); //获取returnvalue参数 return ds; } } } } public static int ExecuteNonQuery(string commd) { using (SqlConnection con = new SqlConnection(connection)) { con.Open(); SqlCommand cmd = new SqlCommand(commd, con); return cmd.ExecuteNonQuery(); } } } }