在程序中,有时数据访问层要调用存储过程,那么,在存储过程中如何调用及如何获取存储过程的输出参数值呢?下面是C#代码的实现 1.运用Command对象 SqlConnection con = new SqlConnection(constring);//constring是连接字符串 con.Open(); string sql = "myproc"; //存储过程名称myproc SqlCommand 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"; //存储过程名称myproc SqlDataAdapter 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();