Ado.net之存储过程的使用

重点是红色标记区域的代码,设置本次执行的是存储过程,如果不设置,默认操作的是sql语句

private void LoadData()
{

string constr = @"database=ItcastCater;server=LAPTOP-2FGC7ARC\Wangjin;user=sa;pwd=sa";
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "usp_gettest";
DataTable dt = new DataTable();
using (SqlDataAdapter Adapter = new SqlDataAdapter(sql, conn))

{
//设置sqlcommand执行的是存储过程
Adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
//增加参数,存储过程中有几个参数,这里就要增加几个参数
SqlParameter[] pms = new SqlParameter[] {
new SqlParameter("@pagesize",SqlDbType.Int){Value=pagesize},
new SqlParameter("@pageindex",SqlDbType.Int){Value=pageindex},
new SqlParameter("@recordcount",SqlDbType.Int){Direction=ParameterDirection.Output},
new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
};
Adapter.SelectCommand.Parameters.AddRange(pms);
Adapter.Fill(dt);
//数据绑定
label1.Text = pms[2].Value.ToString();
label2.Text = pms[3].Value.ToString();
//执行
this.dataGridView1.DataSource = dt;
}
}
}

猜你喜欢

转载自www.cnblogs.com/wangjinya/p/10038250.html