This is an old article, but I still like the way the example comes together. It is a straightforward console application that connects to an MS Access database through the OleDb provider.

Original note from the old post: the actual draft date was 2008-05-19.

The idea was simple: create an Access database, connect to it from a console application, insert a record, then read it back and print the result.

First, create a table named MyTable with these fields:

  • Id as AutoNumber
  • FullName as Text

That should look something like this:

Next, create a new console application in Visual Studio:

Add a reference to System.Configuration:

Then add an app.config file so you can keep the connection string outside the code:

Once that is in place, the connection helper can look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
private bool connect_db()
{
    bool returnval = false;

    if (ConnectionProvider == null)
        ConnectionProvider = new OleDbConnection(ConnectionString);

    if (ConnectionProvider.State == System.Data.ConnectionState.Closed)
    {
        returnval = true;
        ConnectionProvider.Open();
    }

    return returnval;
}

Then in Main() you can connect, insert a record, query it back, and print the result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
ms.connect_db();

OleDbCommand cmd = null;
OleDbDataReader rds;

cmd = new OleDbCommand("insert into MyTable (fullname) values ('my name is cliper');", ms.ConnectionProvider);
cmd.ExecuteNonQuery();

cmd = new OleDbCommand("select id, fullname from MyTable;", ms.ConnectionProvider);
rds = cmd.ExecuteReader();

while (rds.Read())
    Console.WriteLine("[Id]\t[Test Name]\n{0}\t{1}", rds["id"], rds["fullname"]);

rds.Close();
ms.ConnectionProvider.Close();

That is really the whole point of the sample: use a simple console application with ADO.NET and the OleDb provider to work with an Access database.

Source downloads from the original post: