有一个GridView1
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
用DataReader读数据库信息
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "Data Source=.;DataBase=Bu;UID=sa;PWD=密码;"; //连接字符串
SqlCommand cmd = new SqlCommand("select * from tableA",sqlCon); //sql语句或存储过程
sqlCon.Open();
SqlDataReader dr = cmd.ExecuteReader();
将内存中的数据流作为数据源绑定给GridView
GridView1.DataSource = dr;
GridView1.DataBind();
炒鸡简单吧,额呵呵~
同理,绑定DataSet和DataTable也是一样的。将DataSet填充数据集(一个DataSet可有多个DataTable)
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "Data Source=.;DataBase=Bu;UID=sa;PWD=密码;"; //连接字符串
SqlCommand cmd = new SqlCommand("select * from tableA",sqlCon); //sql语句或存储过程
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select top 3 * from menber", sqlCon);
DataSet ds = new DataSet();
da.Fill(ds, "menber");
DataTable dt = ds.Tables[0]; //DataSet里第一张表赋给DataTable
将ds或者dt绑定给GridView
GridView1.DataSource = ds; //也可以是dt
GridView1.DataBind();
炒鸡简单吧,额呵呵~