Implement the Combox binding database in C #

zhaozj2021-02-16  138

Using the database bosed control is a common method for WinForm programming, the following is my experience and method:

Using system;

Using system.windows.form; using system.data.oledb; using sybase.datawindow; using system.data.sqlclient;

Namespace Data {///

/// Form1 summary description. ///

Public Class Form1: System.Windows.Forms.form

{

................ / / Other independent control

Private system.data.oledb.oledbconnection con; private system.data.oledb.oledbcommand Command;

Private system.data.oledb.oledbdatareader reader;

Private system.windows.Forms.comBobox ComboBox1;

................ // Here is the form design code

Private void form1_load (object sender, system.eventargs e) {try {con = New OLEDBConnection (@ "provider = microsoft.jet.oledb.4.0; data source = d: /pems/data.mdb"); // Create a database Joint, this use of Access database con -.open (); command = con.createCommand (); command.commandtext = "select t_name from teacher"; // Create Query Statement = Command.executeReader (); while (Reader). Read ()) {this.comboBox1.items.addrange (new object [] {reader ["t_name"]}; // bonded data} reader.close (); con.close ();} catch (Exception EX) ) {MessageBox.show (ex.toString ());

}

} Similarly, if you want to bind other controls, such as CheckListBox, you can use the same method whistle to remove data from the database, then use the control .Items.addRange method to add it.

转载请注明原文地址:https://www.9cbs.com/read-7792.html

New Post(0)