DropDownList 컨트롤에 데이터를 추가하는 두 가지 방법
저자:Eve Cole
업데이트 시간:2009-07-01 16:09:58
방법:
1 문자열 sql="SELECT * FROM NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection conn=new OleDbConnection();
4
5 conn.ConnectionString=연결문자열;
6 연결.열기();
7
8 OleDbCommand cmd = new OleDbCommand(Sql,Conn);
9 OleDbDataReader dr= cmd.ExecuteReader();
10
11 //将数据添加到 DropDownList
12 동안(dr.Read())
13 {
14 ddl.Items.Add(new ListItem(dr["ClassName"].ToString(),dr["ClassId"].ToString()));
15 }
16 dr.닫기();
17
18cmd.Dispose();
19 연결.닫기;
20 연결.Dispose();
21
방법이:
1 문자열 sql="SELECT * FROM NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection conn=new OleDbConnection();
4 conn.ConnectionString=연결문자열;
5 연결.열기();
6
7 데이터세트 ds=new DataSet();
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