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