none
Zugriff auf accde-AccessDB RRS feed

  • Frage

  • Hallo

    Ich finde viele Bespiele, wie man mit C# auf eine Access accdb-Datenbank und ihre Tabellen zugreift. Ich möchte aber auf eine Tabelle in einer kompilierten Access DB accde zugreifen. Ist das überhaupt möglich? Wenn ja, wie kann ich die Daten auslesen?

    Es geht mir um eine Zelle in einer Tabelle, der die Version der Datenbank enthält. Damit will ich herausfinden, ob die aktuelle Version auf dem Netz neuer ist als die lokal installierte und diese gegebenenfalls ersetzen.

    Vielen Dank, wenn jemand mir einen Tipp geben könnte.


    Danke und Gruss Thomas

    Donnerstag, 30. Dezember 2021 14:38

Antworten

  • Hi Thomas,
    für den Zugriff auf die Daten lässt sich eine accde genau so öffnen wie eine accdb. Hier mal eine Demo als WindowsForms-Anwendung:

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
      public partial class Form28 : Form
      {
        public Form28()
        {
          InitializeComponent();
          this.Load += Form1_Load;
          this.Button1.Click += Button1_Click;
          this.Button2.Click += Button2_Click;
        }
    
        #region simulate Designer
        private Button Button1 = new Button() { Text = "Load data", Dock = DockStyle.Top };
        private Button Button2 = new Button() { Text = "Save data", Dock = DockStyle.Top };
        private DataGridView DataGridView1 = new DataGridView() { Dock = DockStyle.Fill };
    
        private void Form1_Load(object sender, EventArgs e) => this.Controls.AddRange(new Control[] { DataGridView1, Button2, Button1 });
        #endregion
    
        // ConnectionString with path (data source) to access file in directory of exe
        private string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Test.accde;";
    
        // Connection to access file, instantiating on first usung
        private OleDbConnection _connection;
        private OleDbConnection Connection
        {
          get
          {
            if (this._connection == null) this._connection = new OleDbConnection(connString);
            return this._connection;
          }
        }
    
        // DataAdatper for loading and updating data in access file
        private OleDbDataAdapter _dataAdapter;
        private OleDbDataAdapter DataAdapter
        {
          get
          {
            if (this._dataAdapter == null)
            {
              this._dataAdapter = new OleDbDataAdapter("SELECT * FROM Tab2", this.Connection);
              var cb = new OleDbCommandBuilder(this._dataAdapter);
              cb.QuotePrefix = "[";
              cb.QuoteSuffix = "]";
            }
            return this._dataAdapter;
          }
        }
    
        // buffer for data
        private DataTable Data;
    
        // Load data from access file
        private void Button1_Click(object sender, EventArgs e)
        {
          Data = new DataTable();
          DataAdapter.Fill(Data);
          DataGridView1.DataSource = new BindingSource() { DataSource = Data };
        }
    
        // Save changed, added and deleted data
        private void Button2_Click(object sender, EventArgs e) { if (Data != null) this.DataAdapter.Update(Data); }
      }
    }


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Donnerstag, 30. Dezember 2021 16:29

Alle Antworten

  • Hi Thomas,
    für den Zugriff auf die Daten lässt sich eine accde genau so öffnen wie eine accdb. Hier mal eine Demo als WindowsForms-Anwendung:

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
      public partial class Form28 : Form
      {
        public Form28()
        {
          InitializeComponent();
          this.Load += Form1_Load;
          this.Button1.Click += Button1_Click;
          this.Button2.Click += Button2_Click;
        }
    
        #region simulate Designer
        private Button Button1 = new Button() { Text = "Load data", Dock = DockStyle.Top };
        private Button Button2 = new Button() { Text = "Save data", Dock = DockStyle.Top };
        private DataGridView DataGridView1 = new DataGridView() { Dock = DockStyle.Fill };
    
        private void Form1_Load(object sender, EventArgs e) => this.Controls.AddRange(new Control[] { DataGridView1, Button2, Button1 });
        #endregion
    
        // ConnectionString with path (data source) to access file in directory of exe
        private string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Test.accde;";
    
        // Connection to access file, instantiating on first usung
        private OleDbConnection _connection;
        private OleDbConnection Connection
        {
          get
          {
            if (this._connection == null) this._connection = new OleDbConnection(connString);
            return this._connection;
          }
        }
    
        // DataAdatper for loading and updating data in access file
        private OleDbDataAdapter _dataAdapter;
        private OleDbDataAdapter DataAdapter
        {
          get
          {
            if (this._dataAdapter == null)
            {
              this._dataAdapter = new OleDbDataAdapter("SELECT * FROM Tab2", this.Connection);
              var cb = new OleDbCommandBuilder(this._dataAdapter);
              cb.QuotePrefix = "[";
              cb.QuoteSuffix = "]";
            }
            return this._dataAdapter;
          }
        }
    
        // buffer for data
        private DataTable Data;
    
        // Load data from access file
        private void Button1_Click(object sender, EventArgs e)
        {
          Data = new DataTable();
          DataAdapter.Fill(Data);
          DataGridView1.DataSource = new BindingSource() { DataSource = Data };
        }
    
        // Save changed, added and deleted data
        private void Button2_Click(object sender, EventArgs e) { if (Data != null) this.DataAdapter.Update(Data); }
      }
    }


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Donnerstag, 30. Dezember 2021 16:29
  • Hallo Peter

    Ich war eine Weile abwesend, sorry für die verspätete Antwort.

    Vielen Dank für Deine Antwort. So funktioniert es.



    Danke und Gruss Thomas

    Dienstag, 11. Januar 2022 11:02