简体   繁体   中英

Using Microsoft Access 2007 database with Visual C# 2010

I have finished designing my C# application which stores employees data in an Access database. The program works perfectly when I install Microsoft Office 2007 on my machine, but causes an error if I uninstall Office 2007. Now, I wonder if there is a method which enables my program to connect to the Access database without installing the whole Microsoft office 2007.

using System.Data.OleDb;

If your connection string uses the Microsoft.ACE.OleDb.12.0 provider then you need the Microsoft Access Database Engine installed on the client.

At the link provided you could find the installation of the two versions of this provider.
One for 32bit and one for 64bit.

The right one to use depends by the Target CPU defined in your app and by the operating system bitness. Of course on a 32bit OS you cannot install a 64bit provider while the reverse is possible.

If your app is compiled for x86 as Target CPU then you need the 32bit version and you could install your app also on 64bit systems. If your app is compiled for x64 Target CPU then you need the 64bit version and you could run only on 64bit systems. If your app is compiled for Any CPU then you should decide at install time which version of ACE.OleDb.12.0 to install depending on the bitness of the operating system.

If this is not enough to give you headache then things start to look very bad if there is an Office already installed on the client machine.

Office installs automatically the provider compatible with its version (32 or 64 bit) and you cannot install a different version of these libraries side by side with the other one.

So what are your best options?

  • Write your app for x86 Target CPU and provide a dedicated setup for systems where Office 64 bit is installed or ask your customer to change Office version. (I'm kidding)
  • Write your app for AnyCPU Target , check at install time the OS bitness and choose the appropriate version of the engine. On 64bit systems hope that your customer has not installed a 32bit version of Office.
  • Use the Access 2003 format (mdb) and compile for x86 Target CPU. This approach requires the Microsoft.Jet.OleDb.4.0 provider that should be already available on any target client. It doesn't require to install anything and it should work flawlessy on any system.
  • Switch to SQLite or Sql Server Compact Edition or Sql Server LocalDB . These options requires a complete retesting of the application, database schema migration and probably code changes. Still, dedicated libraries, are needed to distribute

Personally for small, single user applications, with limited amount of data (less than 1gb) and with integration with other Office applications I use the Access 2003 MDB solution.

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