DataSet,DataTable绑定gridview
前台
<asp:Button ID="Button1" runat="server" Text="显示" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="显示1" />
后台
protected void Button1_Click(object sender, EventArgs e)
{
string constr=@"Data source=PC-20121107KEFI\MSAS;initial catalog=School;user id=sa;password=12345";
string sql="select * from T_student";
DataSet ds = new DataSet();
SqlDataAdapter adapter= new SqlDataAdapter(sql,constr);
adapter.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
string constr = @"Data source=PC-20121107KEFI\MSAS;initial catalog=School;user id=sa;password=12345";
string sql = "select * from T_student";
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql, constr);
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}