ASP.net GridView控件的使用

来源:百度知道 编辑:UC知道 时间:2024/05/15 11:04:35
我说一下具体要求吧,就是在index.aspx.cs中编写代码连接本地SQL server数据库,使在index.aspx中的GridView控件显示数据库中的表的数据,具体要在index.aspx.cs写什么呢?

SqlConnection conn = new SqlConnection("数据库连接字符串");
string sqlstr="select * from 表名";
SqlCommand cmd = new SqlCommand(sqlstr,conn);
SqlDataAdapter dapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dapt.Fill(ds,"ds中的表名,自己取");
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();

SqlConnection conn = new SqlConnection("数据库连接字符串");
string sql = "select * from 表名";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader sdr = cmd.ExecReader();
this.DataGrid1.DataSource=sdr;
this.DataGrid1.DataBind();

msdn是最好的资料
http://msdn2.microsoft.com/zh-cn/library/4w7ya1ts(VS.80).aspx