数据控件数据绑定方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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();
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();
12
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();
12
--------------------------------------------------------------------------
今天,明天就在此刻!此刻奋斗,今天耕耘,明天收获,明天辉煌!此刻放弃, 今天无为,明天饥饿,明天黯淡!
JimmyCai