Category Archives: ODBC

Accessing Data using DBISAM ODBC Driver with C#.NET

Hello again,

My work is always an adventure, and it’s fun too. I’m currently working with a project where I need to connect to a DBISAM Tables (dat files) to get some data so I thought is a good idea to create a post about it.

The first thing you need to do is to download the DBISAM Drivers (in case you don’t have it installed), I downloaded from here (you need to register first!)

I downloaded the file DBISAM-ODBC-TRIAL – DBISAM ODBC Trial

After the installation we need to configure our data source, for this we will use the ODBC Data Source Administrator we have in Windows, just go to Administrative Tools and open the ODBC (Data Sources) program.

NOTES (In case you have Windows 7 64-bit)
Because I have the Windows 7 Pro 64-bit version I need to use the 32-bit ODBC Data Source Administrator Tool because the drivers are in 32-bits for more information please check this article.

So we are ready to use this data source in our program. First we need to import the libraries we will use.

using System;
using System.Data.Odbc;

As you can see I imported the ODBC library from System.Data Assembly to use this class to connect to our data source. The next step is to create the OdbcCommand that have the SQL query we want to use.

string queryString = "SELECT * FROM machine";
OdbcCommand command = new OdbcCommand(queryString);

Then if we want to access to the data within the data source we need to create a connection, right?

string connectionStg = "PROVIDER=MSDASQL;DSN=MacDB;UID=admin;PWD=;";
OdbcConnection connection = new OdbcConnection(connectionStg)

Now we have a connection using a connection string we created (please note that the DSN is the name we created for the data source in the ODBC Data Source).

The final step is to execute our query and display the results:

using (OdbcConnection connection = new OdbcConnection(connectionStg))
{
    command.Connection = connection;
    connection.Open();
    OdbcDataReader reader = command.ExecuteReader();

    int fCount = reader.FieldCount;
    while (reader.Read())
    {
        for (int i = 0; i < fCount; i++)
        {
            Console.WriteLine(reader.GetValue(i).ToString());
        }
    }

    reader.Close();
    command.Dispose();
}

And that’s it, working and clean!