SQL binding
Private void form1_load (Object sender, system. eventargs E)
{
// Bind the data table YG to combox1 during formload
Sqlconnection conn = new sqlconnection ("Server =.; uid = sa; Pwd =; database = GZ ");
Conn. open ();
Sqldataadapter da = new sqldataadapter ("select * From YG", Conn );
Dataset DS = new dataset ();
Da. Fill (DS, "YG ");
Datarow DR = Ds. Tables ["YG"]. newrow ();
Dr ["ygid"] = "0 ";
Dr ["ygname"] = "-select -";
DS. Tables ["YG"]. Rows. insertat (DR, 0 );
This. combobox1.datasource = Ds. Tables ["YG"];
This. combobox1.displaymember = "ygname"; // displayed Column
This. combobox1.valuemember = "ygid"; // data column. The secondary column is displayed in selectedvalue.
Conn. Close ();
}
Access Database binding
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Data. oledb;
Namespace windowsapplication1
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
String strcon = "provider = Microsoft. Jet. oledb.4.0; Data Source = company. mdb ";
Oledbconnection myconn = new oledbconnection (strcon );
String strcom = "select * from Company ";
// Create a dataset
Dataset mydataset = new dataset ();
Myconn. open ();
// Use oledbdataadapter to obtain a dataset
Oledbdataadapter mycommand = new oledbdataadapter (strcom, myconn );
// Bind dataset to the company data table
Mycommand. Fill (mydataset, "company ");
// Close this oledbconnection
Myconn. Close ();
This. cmbname. datasource = mydataset;
This. cmbname. displaymember = "company. Name ";
This. cmbname. valuemember = "company. Name ";
}
Private void cmbname_selectedindexchanged (Object sender, eventargs E)
{
String strcon = "provider = Microsoft. Jet. oledb.4.0; Data Source = company. mdb ";
Oledbconnection myconn = new oledbconnection (strcon );
Myconn. open ();
String strsql = "select * from company where company. Name = '" + this. cmbname. Text + "'";
Oledbcommand COM = new oledbcommand (strsql, myconn );
Oledbdatareader READ = com. executereader ();
If (read. Read ())
{
Address. Text = read ["Address"]. tostring ();
Tel. Text = read ["tel"]. tostring ();
}
}
}
}