net operating access database sample share

  • 2020-11-25 07:14:04
  • OfStack


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>
///AccessDB  Summary description of 
/// </summary>
public class AccessDB
{
    public AccessDB()
    {
        //TODO:  Add the constructor logic here 
    }
    /// <summary>
    ///  read Access The 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 \ The application .mdb";
        OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
        //2 , open the connection 
        odcConnection.Open();
        // To establish SQL The query 
        OleDbCommand odCommand = odcConnection.CreateCommand();
        //3 , enter the query statement 
        odCommand.CommandText = "SELECT table. Serial number , table.[123], table.[456] FROM [table];";
        // Establish a reading 
        OleDbDataReader odrReader = odCommand.ExecuteReader();
        // Query and display the 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 button1_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 \ The application .mdb";
        OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
        //2 , open the connection 
        odcConnection.Open();
        // To establish SQL The query 
        OleDbCommand odCommand = odcConnection.CreateCommand();
        //3 , enter the query statement 
        odCommand.CommandText = "INSERT INTO [table]([123],[456]) VALUES('88','88' )";
        // perform 
        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 \ The application .mdb";
        OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
        //2 , open the connection 
        odcConnection.Open();
        // To establish SQL The query 
        OleDbCommand odCommand = odcConnection.CreateCommand();
        //3 , enter the query statement 
        string sql = "DELETE table. Serial number  FROM [table] WHERE (((table. Serial number )=" + this.listView1.SelectedItems[0].SubItems[1].Text + "))";
        odCommand.CommandText = sql;
        // perform 
        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 \ The application .mdb";
        OleDbConnection odcConnection = new OleDbConnection(sAccessConnection);
        //2 , open the connection 
        odcConnection.Open();
        // To establish SQL The query 
        OleDbCommand odCommand = odcConnection.CreateCommand();
        //3 , enter the query statement 
        string sql = "UPDATE [table] SET 123='11',456='11'  WHERE (((table. Serial number )=" + this.listView1.SelectedItems[0].SubItems[1].Text + "))";
        odCommand.CommandText = sql;
        // perform 
        odCommand.ExecuteNonQuery();
        // Close the connection 
        odcConnection.Close();
        this.listView1.Items.Clear();
        ReadAccessData();
    }
}


Related articles: