Duas maneiras de adicionar dados ao controle DropDownList
Autor:Eve Cole
Data da Última Atualização: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 conexão.Open();
7
8 OleDbCommand cmd = novo OleDbCommand(Sql,Conn);
9 OleDbDataReader dr= cmd.ExecuteReader();
10
11 //将数据添加到 DropDownList
12 enquanto(dr.Read())
13 {
14 ddl.Items.Add(new ListItem(dr["ClassName"].ToString(),dr["ClassId"].ToString()));
15}
16 dr.Fechar();
17
18 cmd.Dispose();
19 conexão.Fechar;
20 conexão.Dispose();
21
方法二:
1 string sql="SELECT * FROM NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection conn=new OleDbConnection();
4 conn.ConnectionString=ConnectionString;
5 conexão.Open();
6
7 DataSet ds=novo DataSet();
8 OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
9 da.Fill(ds,"ClassTable");
10
11 //将数据添加到 DropDownList
12ddl.DataSource=ds;
13 ddl.DataTextField = "ClassName";
14 ddl.DataValueField = "ClassID";
15 ddl.DataBind();
16
17ds.Dispose();
18 conexões.Fechar;
19 conexão.Dispose();
20
21
http://www.cnblogs.com/meil/archive/2006/10/19/533812.html