C # winform ComboBox dynamic drop-down list, ComboBox dynamically binds data in the database Code
1 Public Void Insertcomshopid (ComboBox CB)
2 {
3 CB. Items. Clear (); // Clear combox
4 Sqlcommand cmd = New Sqlcommand ( " Select name from shop " , DB. getcon ()); // Construct the connection string and open the database connection
5 Sqldatareader SDR = Cmd. executereader ();
6 While (SDR. Read ())
7 {
8CB. Items. Add (SDR [0]. Tostring ());//Read data cyclically
9} // End Block while
10
11 SDR. Close (); // Close a dataset
12 DB. getcolse (); // Close database connection
13 }
14
Or
Code
1 String Connectionstring = " Data Source = dzjc_2005; user = KK; Password = KK; " ; // Write connection string
2 Oracleconnection Conn = New Oracleconnection (connectionstring ); // Create a new connection
3
4 Conn. open ();
5 Oraclecommand cmd = Conn. createcommand ();
6 Cmd. commandtext = " Select rname, did, rchannel from nwvideo. n_road " ; // Write SQL statements here
7 Oracledatareader Dr = Cmd. executereader (); // Create an oracledatereader object
8
9 If (Dr. hasrows)
10 {
11 Cmbchannelselect. Items. Clear (); // Clear combox
12 While (Dr. Read ())
13 {
14Cmbchannelselect. Items. Add (Dr [2]. Tostring ());//Cyclic read area data
15
16}
17 }
18