Tuesday, July 14, 2009

Read MS Access through .Net

Hi,

I had a requirement to create a application to retrieve data from legacy MS-Access application.
I was looking for a sample in google. could'nt find one. So posting it so that it could be useful for people who is looking for the same.

I have created a DNS for the mdb file. used System.Data.Odbc; and rest are similar to how we do for SQL. following is the sample code.

static void Main(string[] args)

{

//Read Data from Access

OdbcConnection ODBCConnection = new System.Data.Odbc.OdbcConnection("DSN=DNS_Test_MSACCESS;UID=sam;PWD=mam;");

// ODBCConnection= new OdbcConnection(ConnString);

ODBCConnection.Open();

OdbcCommand ODBCCommand = new OdbcCommand("select * from Table1", ODBCConnection);

OdbcDataReader ODBCDataReader = ODBCCommand.ExecuteReader();

string s = "";

while (ODBCDataReader.Read())

{

s = s + "- " + ODBCDataReader.GetString(1);

}

Console.WriteLine(s);

ODBCConnection.Close();

}


No comments: