ComboBox bound to database

Source: Internet
Author: User

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 ();
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.