c# winform comboBox動態下拉式清單,comboBox動態綁定資料庫裡的資料 Code
1public void InsertComShopID(ComboBox cb)
2 {
3 cb.Items.Clear();//清空ComBox
4 SqlCommand cmd = new SqlCommand("select name from shop",DB.Getcon());//構造連接字串並切開啟資料庫連接
5 SqlDataReader sdr =cmd.ExecuteReader();
6 while (sdr.Read())
7 {
8 cb.Items.Add(sdr[0].ToString());//迴圈讀取資料
9 }//end block while
10
11 sdr.Close();// 關閉資料集
12 DB.GetColse();//關閉資料庫連接
13 }
14
或者
Code
1string ConnectionString = "Data Source=dzjc_2005;user=kk;password=kk;";//寫串連串
2 OracleConnection conn = new OracleConnection(ConnectionString);//建立一個新串連
3
4 conn.Open();
5 OracleCommand cmd = conn.CreateCommand();
6 cmd.CommandText = "SELECT rname,did,rchannel FROM nwvideo.n_road ";//在這兒寫sql語句
7 OracleDataReader dr = cmd.ExecuteReader();//建立一個OracleDateReader對象
8
9if (dr.HasRows)
10 {
11 cmbChannelSelect.Items.Clear();//清空ComBox
12 while (dr.Read())
13 {
14 cmbChannelSelect.Items.Add(dr[2].ToString());//迴圈讀區資料
15
16 }
17 }
18