Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. Data. OleDb;
/// <Summary>
/// Summary of AccessDB
/// </Summary>
Public class AccessDB
{
Public AccessDB ()
{
// TODO: add the constructor logic here
}
/// <Summary>
/// Read the Access database
/// </Summary>
Private void ReadAccessData ()
{
// 1. Establish a connection
String sAccessConnection = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = C: \ Documents and Settings \ Administrator. WWW-410ADC78208 \ Desktop \ application. mdb ";
OleDbConnection odcConnection = new OleDbConnection (sAccessConnection );
// 2. Open the connection
OdcConnection. Open ();
// Create an SQL query
OleDbCommand odCommand = odcConnection. CreateCommand ();
// 3. Enter the query statement
OdCommand. CommandText = "SELECT table. No., table. [123], table. [456] FROM [table];";
// Create a read
OleDbDataReader odrReader = odCommand. ExecuteReader ();
// Query and display data
While (odrReader. Read ())
{
ListViewItem item = new ListViewItem ();
Item. SubItems. Add (odrReader [0]. ToString ());
Item. SubItems. Add (odrReader [1]. ToString ());
Item. SubItems. Add (odrReader [2]. ToString ());
This. listView1.Items. Add (item );
}
// Close the connection
OdrReader. Close ();
OdcConnection. Close ();
}
/// <Summary>
/// Add
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#click (object sender, EventArgs e)
{
// 1. Establish a connection
String sAccessConnection = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = C: \ Documents and Settings \ Administrator. WWW-410ADC78208 \ Desktop \ application. mdb ";
OleDbConnection odcConnection = new OleDbConnection (sAccessConnection );
// 2. Open the connection
OdcConnection. Open ();
// Create an SQL query
OleDbCommand odCommand = odcConnection. CreateCommand ();
// 3. Enter the query statement
OdCommand. CommandText = "insert into [table] ([123], [456]) VALUES ('88 ', '88 ')";
// Execute
OdCommand. ExecuteNonQuery ();
// Close the connection
OdcConnection. Close ();
This. listView1.Items. Clear ();
ReadAccessData ();
}
/// <Summary>
/// Delete
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button3_Click (object sender, EventArgs e)
{
// 1. Establish a connection
String sAccessConnection = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = C: \ Documents and Settings \ Administrator. WWW-410ADC78208 \ Desktop \ application. mdb ";
OleDbConnection odcConnection = new OleDbConnection (sAccessConnection );
// 2. Open the connection
OdcConnection. Open ();
// Create an SQL query
OleDbCommand odCommand = odcConnection. CreateCommand ();
// 3. Enter the query statement
String SQL = "DELETE table. FROM [table] WHERE (table. number) = "+ this. listView1.SelectedItems [0]. subItems [1]. text + "))";
OdCommand. CommandText = SQL;
// Execute
OdCommand. ExecuteNonQuery ();
// Close the connection
OdcConnection. Close ();
This. listView1.Items. Clear ();
ReadAccessData ();
}
/// <Summary>
/// Update
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button2_Click (object sender, EventArgs e)
{
// 1. Establish a connection
String sAccessConnection = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = C: \ Documents and Settings \ Administrator. WWW-410ADC78208 \ Desktop \ application. mdb ";
OleDbConnection odcConnection = new OleDbConnection (sAccessConnection );
// 2. Open the connection
OdcConnection. Open ();
// Create an SQL query
OleDbCommand odCommand = odcConnection. CreateCommand ();
// 3. Enter the query statement
String SQL = "UPDATE [table] SET 123 = '11', 456 = '11' WHERE (table. number) = "+ this. listView1.SelectedItems [0]. subItems [1]. text + "))";
OdCommand. CommandText = SQL;
// Execute
OdCommand. ExecuteNonQuery ();
// Close the connection
OdcConnection. Close ();
This. listView1.Items. Clear ();
ReadAccessData ();
}
}