兩種新增資料到DropDownList控制項的方法
作者:Eve Cole
更新時間:2009-07-01 16:09:58
方法一:
1 string sql="SELECT * FROM NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection conn=new OleDbConnection();
4
5 conn.ConnectionString=ConnectionString;
6 conn.Open();
7
8 OleDbCommand cmd = new OleDbCommand(Sql,Conn);
9 OleDbDataReader dr= cmd.ExecuteReader();
10
11 //將資料加入DropDownList
12 while(dr.Read())
13{
14 ddl.Items.Add(new ListItem(dr["ClassName"].ToString(),dr["ClassId"].ToString()));
15}
16.關閉();
17 號
18 cmd.Dispose();
19 康乃狄克州
20 conn.Dispose();
21
方法二:
1 string sql="SELECT * FROM NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection conn=new OleDbConnection();
4 conn.ConnectionString=ConnectionString;
5 conn.Open();
6
7 資料集 ds=new 資料集();
8 OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
9 da.Fill(ds,"ClassTable");
10
11 //將資料加入DropDownList
12 ddl.DataSource=ds;
13 ddl.DataTextField = "類別名稱";
14 ddl.DataValueField = "類別ID";
15 ddl.DataBind();
16
17 ds.Dispose();
18 康乃狄克州
19 conn.Dispose();
20
21
http://www.cnblogs.com/meil/archive/2006/10/19/533812.html