WEB开发网
开发学院WEB开发ASP.NET 两种添加数据到DropDownList控件的方法 阅读

两种添加数据到DropDownList控件的方法

 2006-10-20 17:17:22 来源:WEB开发网   
核心提示:方法一: 1 string sql="SELECT * FROM NewsClass ORDER BY ORDERID DESC"; 2 3 OleDbConnection conn=new OleDbConnection(); 4 5 conn.ConnectionString=Connecti
方法一:
 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  dr.Close();
17
18  cmd.Dispose();
19  conn.Close;
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  DataSet 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 = "ClassName";
14  ddl.DataValueField = "ClassID";
15  ddl.DataBind();
16
17  ds.Dispose();
18  conn.Close;
19  conn.Dispose();
20
21
http://www.cnblogs.com/meil/archive/2006/10/19/533812.html

Tags:添加 数据 DropDownList

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接