C#中如何获取存储过程的输出参数值

来源:互联网 发布:总后勤部职工数据库 编辑:程序博客网 时间:2024/05/16 11:59

在程序中,有时数据访问层要调用存储过程,那么,在存储过程中如何调用及如何获取存储过程的输出参数值呢?下面是C#代码的实现

1.运用Command对象

SqlConnection con = new SqlConnection(constring);//constring是连接字符串con.Open();string sql = "myproc";   //存储过程名称myprocSqlCommand com = new SqlCommand(sql,con);//设置Command对象的类型com.CommandType = CommandType.StoredProcedure;SqlParameter output =            com.Parameters.Add("@i", SqlDbType.Int);  //添加参数//设置参数类型output.Direction = ParameterDirection.Output;//执行com.ExecuteNonQuery();            //获取输出参数值对界面文体框this.textBox1.Text = (output.Value).ToString();//另一种写法//this.textBox1.Text = com.Parameters["@i"].Value.ToString();con.Close();

2.运用DataAdapter对象

DataSet ds = new DataSet();string sql = "myproc";   //存储过程名称myprocSqlDataAdapter da = new SqlDataAdapter(sql, con);//添加参数SqlParameter accp =        da.SelectCommand.Parameters.Add("@i", SqlDbType.Int);//设置参数类型accp.Direction = ParameterDirection.Output;   //设置命令对象类型         da.SelectCommand.CommandType = CommandType.StoredProcedure;//填充数据da.Fill(ds);this.dataGridView1.DataSource = ds.Tables[0];//获取参数值给文本框this.textBox2.Text = da.SelectCommand.Parameters[0].Value.ToString();



原创粉丝点击