DataGridView displaying database contents and updating content to database

Source: Internet
Author: User

1: Class access, which contains the read database, updates the database method:

Class Access
{
Private OleDbConnection Olecon = null;//Connection Database
Private OleDbDataAdapter Oleadapter = null;//Converter
Private DataSet ds = new DataSet ();//Data set
public void AccessRead (String strFilePath, string Strsql,object obj)
{
String str = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strFilePath;
Try
{
Olecon = new OleDbConnection (str);//Connect to Database
if (olecon.state = = ConnectionState.Open) {olecon.close ();}
Olecon.open ();
//
Oleadapter = new OleDbDataAdapter (strSQL, Olecon);//EXECUTE statement
//
Oleadapter.fill (DS);//Populate Data set DS
//
DataGridView DGV = (DataGridView) obj;
DGV. DataSource = ds. Tables[0];
//
Olecon.close ();
}
catch (Exception ex)
{
MessageBox.Show (ex. ToString (), "hint");
}

}
//
public void Accessupdate ()
{

Automatically generate single-table commands for reconciling changes to the DataSet with the associated database
OleDbCommandBuilder OLECB = new OleDbCommandBuilder (oleadapter);//create command rebuild Object
Try
{
Oleadapter.update (DS);//Update data
}
catch (Exception ex)
{
MessageBox.Show (ex. ToString (), "hint");
}
}
}

2:form interface Operation:

Access AC = new access ();
Read database
private void Btnopen_click (object sender, EventArgs e)
{
String strfilepath=application. StartupPath + @ "\db\database.mdb";
String chartname = "Oracleinfo";
String strSQL = "SELECT * from" + Chartname + "ORDER by ID ASC";
Ac. AccessRead (strFilePath, strSQL, DGV1);
}
Update Database
private void Btnsave_click (object sender, EventArgs e)
{
Ac. Accessupdate ();
MessageBox.Show ("Update succeeded", "hint");
}

3: Effect

1: When clicking the "Open" button, see the database loaded into the control DataGridView.

2: When modifying the database name "24" in column 1th to "20171128, click" Save "to display" Save succeeded ".

DataGridView displaying database contents and updating content to database

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.