标题: C#调用sqlserver存储过程.总是会忽略掉一些小的细节
- qianbao 2008-04-19 15:43 阅读:83
- 评论:0 | 添加评论
小的细节问题,总是会被忽略掉,哎
SqlConnection con = conn.genCon();
con.Open();
DataSet ds = new DataSet();
// 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;
this.GridView1.DataBind();
//返回多个结果集合
ad.NextResult();
this.GridView2.DataSource = ad;
this.GridView2.DataBind();
con.Close();
//获得返回值(一定要在con.close()后)
string p2 = p.Value.ToString();
this.Label1.Text = p2;
//获得输出参数值
this.Label2.Text = sp.Value.ToString();
再加个具体的例子吧.备用
public ArrayList getlist(int page, int pagesize, int cid, out int count)
{
con = Conn.getcon();
con.Open();
cmd = new SqlCommand("getarticlelist",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@page", SqlDbType.Int).Value = page;
cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = pagesize;
cmd.Parameters.Add("@cid", SqlDbType.Int).Value = cid;
cmd.Parameters.Add("@count", SqlDbType.Int).Direction = ParameterDirection.Output;
SqlDataReader reader = cmd.ExecuteReader();
ArrayList list = new ArrayList();
Article at;
while (reader.Read())
{
at = new Article();
at.id = Convert.ToInt32(reader["id"]);
at.cid = Convert.ToInt32(reader["cid"]);
at.cname = reader["cname"].ToString();
at.title = reader["title"].ToString();
at.postdate = Convert.ToDateTime(reader["postdate"]).ToString("yyyy-MM-dd");
list.Add(at);
}
con.Close();
count = Convert.ToInt32(cmd.Parameters["@count"].Value);
return list;
}
添加评论
返回顶部 | 返回首页