Access Database from CSharp

A small example how to access a Microsoft Access database from C#.

Ein kleines Beispiel wie man auf eine Microsoft Access Datenbank in C# zugreift:

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Access-Datei (*.mdb)|*.mdb|Alle Dateien (*.*)|*.*";

if (dlg.ShowDialog() == DialogResult.OK)
{
    _conn = new OleDbConnection();

    String connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dlg.FileName + ";Persist Security Info=True";
    string sql = "SELECT * FROM Table";

    _conn.ConnectionString = connection;
    _conn.Open();

    OleDbCommand cmd = new OleDbCommand(sql, _conn);
    OleDbDataReader reader = cmd.ExecuteReader();

    String s = "";

    while (reader.Read())
    {
        s += reader["id"] + " - ";
        s += reader["columnname"] + " - ";
    }

    s += Environment.NewLine;
    s += "Datenbank geöffnet";

    _conn.Close();
    Console.Writeln(s);
}


Datenschutzerklärung | Impressum