首先要得到 SqlConnection对象的实例 con , 下面会用到(至于怎么连数据库,这不用我说了吧)
// sql语句
//SqlCommand cmd = new SqlCommand("select * from bbsboards where boardsortid=1", con);
//SqlDataAdapter ad = new SqlDataAdapter("select * from bbsboards where boardsortid=1", con);
//ad.Fill(ds);
//存储过程
//(1)无参数
//SqlCommand cmd = new SqlCommand("getBoards", con);
//cmd.CommandType = CommandType.StoredProcedure;
//(2)参数(有输入参数,输出参数并且有两个返回结果集)
SqlCommand cmd = new SqlCommand("getBoardsbySortId", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@sortid",1));
//设置输出参数
SqlParameter sp = new SqlParameter();
sp.DbType = DbType.Int32;
sp.Direction = ParameterDirection.Output;
sp.ParameterName = "@a";
cmd.Parameters.Add(sp);
//设置返回值
SqlParameter p = new SqlParameter();
p.DbType = DbType.Int32;
p.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(p);
SqlDataReader ad = cmd.ExecuteReader();
this.GridView1.DataSource = ad; //在这里我直接把结果集绑定在 gridview 上
this.GridView1.DataBind();
//返回多个结果集合
ad.NextResult();
this.GridView2.DataSource = ad; //在这里我直接把结果集绑定在 gridview 上
this.GridView2.DataBind();
con.Close();
//获得返回值(一定要在con.close()后)
p.Value.ToString();
//获得输出参数值
sp.Value.ToString();
ExecuteReader的返回值绑定GridView
最新推荐文章于 2020-08-25 16:26:33 发布