Tuesday, October 23, 2018

Connect to a Microsoft Access Database using .NET Core


Install the following packages via NuGET:
  • Microsoft.Office.Interop.Access
  • Microsoft.Office.Interop.Access.Dao

Include the assemblies as follows:

using Microsoft.Office.Interop.Access;

using Microsoft.Office.Interop.Access.Dao;


Sample of retrieving data in Main program:

var accessApp = new Application();
accessApp.OpenCurrentDatabase(@"C:\Users\OlivierSin\Desktop\RAS.mdb", false, "******");
Database cdb = accessApp.CurrentDb();

Microsoft.Office.Interop.Access.Dao.Recordset result = cdb.OpenRecordset("SELECT username FROM users", Microsoft.Office.Interop.Access.Dao.RecordsetTypeEnum.dbOpenSnapshot);

while(!result.EOF)
{
Console.WriteLine(result.Fields["username"].Value);
result.MoveNext();
}
result.Close();
accessApp.Quit();

No comments:

Post a Comment