Datalist ,gridview,repeater 数据绑定方法
1
2 SqlConnection con=new SqlConnection();
3 con.ConnectionString="server=(local);database=YourDataBase;Trusted_Connection=yes";
4 con.Open();
5 SqlCommand com=new SqlCommand();
6 com.CommandText="select * from YourTable";
7 com.Connection=con;
8 DataGrid1.DataSource=com.ExecuteReader();
9 //还可以使用数据阅读器SqlDataReader(不能使用他的构造函数),代码如下:
10 //SqlDataReader dr=com.ExecuteReader();
11 //DataGrid1.DataSource=dr;
12 //当然还可以使用ADO.NET的核心数据集和数据适配器;
13 //SqlDataAdapter ada=new SqlDataAdapter(com.CommandText,con);
14 //DataSet set1=new DataSet();
15 //ada.Fill(set1);
16 //DataGrid1.DataSource=set1;
17 //最后计算数据绑定表达式,
18 DataGrid1.DataBind();
Checkboxlist,radiobuttonlist 数据绑定
1
2 SqlConnection con = new SqlConnection("server=.;database=db_Trade;uid=sa;pwd=;");
3 con.Open();
4 SqlDataAdapter sda = new SqlDataAdapter("select top 10 * from tb_Speciality", con);
5 DataSet ds = new DataSet();
6 sda.Fill(ds);
7 this.CheckBoxList1.DataSource = ds.Tables[0];
8 this.CheckBoxList1.DataTextField = "speciality_name";
9 this.CheckBoxList1.DataValueField = "speciality_id";
10 this.CheckBoxList1.DataBind();
11 con.Close();