. Net access Database example sharing

Source: Internet
Author: User

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

Related Article

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.