简体   繁体   中英

How to query MS Access database with C# application?

I have an application that uses Microsoft.Office.Interop.Excel to pull data from an Excel workbook. The information in the Excel workbook has been moved to an Access database, and I have been tasked to update the application, so that it will look for the data in the database. I found the Microsoft.Office.Interop.Access reference, but how do I use it to open the database and run queries? The documentation on this on MSDN seems to be somewhat lacking. Maybe I'm looking in the wrong place... :\\ The C# application uses .NET 3.5. Thanks.

I'm not going to repeat everything that's in the MSDN, there's a great walk through right here: http://msdn.microsoft.com/en-us/library/ms971485.aspx

However, do note that you don't need to use the interop assemblies, they are horribly slow, difficult to use and well just a PIA in general.

As the MSDN article shows, everything you want to do can be done using ADO.NET.

You'll need to add the Access DB as a datasource.

This is pretty straightforward; see this example: http://msdn.microsoft.com/en-us/library/bb655884%28v=vs.90%29.aspx

Once you've got your connection in place, you can either use SQL directly or create table adapters with methods which you call to perform whatever it is you need to do /w the DB.

There are a slew of these "How Do I..." items related to DB access on this MSDN page: http://msdn.microsoft.com/en-us/library/ms186197%28v=VS.90%29.aspx

The easiest way is to use ADO.Net to access it just like you would any other database. Create an OleDbConnection object passing in an appropriate conenction string. Here's an example:

var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM