如何打开在C#与Microsoft Access数据库连接 [英] How to open connection with Microsoft Access database in C#

查看:708
本文介绍了如何打开在C#与Microsoft Access数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Microsoft Access创建我的数据库。这里是我的代码:

 静态字符串构造方法=供应商= Microsoft.Jet.OLEDB.4.0; 
+数据源= MyData.accdb;
OleDbConnection的康恩=新的OleDbConnection(构造方法);
DataSet的数据集1 =新的DataSet();
串SQLstr =从TABEL选择*;
OleDbDataAdapter的DataAdapter1;
Conn.Open();



我得到这个异​​常:



<块引用>

类型'System.Data.OleDb.OleDbException'未处理的异常出现在system.data.dll



信息:无法识别的数据库格式



解决方案

您正在试图打开与Access 2007/2010(ACCDB)创建的Access数据库与OLEDB提供程序能够理解与Access只​​创建MDB文件2003



要使用的供应商是 Microsoft.ACE.OleDB.12.0 应该与Office的最新版本已经安装。



在替代你可以从的 Microsoft Access数据库引擎结果
只是要注意下载适合您使用平台(x86或x64)的版本。



这就是连接字符串使用

 提供程序= Microsoft.ACE.OLEDB.12.0;数据源= MyData.accdb; 

请注意:请记住,如果你有安装Office时,相应的提供了Office相同位数,因此与Office 64位来自于64位版本的ACE与同为32位。这种情况在您的应用程序的部署提出了一个非常棘手的问题。你需要你的应用程序具有相同位数或编译你将无法使用所安装的提供者(或者disinstall不兼容的Office版本并重新安装兼容的一当然,如果你应该能够说服你的客户的办公室是不兼容的应用程序: - )。


I'm using Microsoft Access to create my database. Here's my code:

static string Constr = "Provider=Microsoft.Jet.OLEDB.4.0;" 
                     + "Data Source = MyData.accdb";
OleDbConnection Conn = new OleDbConnection(Constr);
DataSet DataSet1 = new DataSet();
string SQLstr = "Select * from Tabel";
OleDbDataAdapter DataAdapter1;
Conn.Open();

I'm getting this exception:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Unrecognized database format

解决方案

You are trying to open an Access database created with Access 2007/2010 (accdb) with an OleDb provider that can understand only MDB files created with Access 2003

The provider to use is Microsoft.ACE.OleDB.12.0 that should be already installed with the latest version of Office.

In alternative you could download the appropriate bits from the Microsoft Access Database Engine
Just pay attention to download the version appropriate for the platform (x86 or x64) you use.

And that's the connection string to use

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MyData.accdb";

NOTE: Keep in mind that if you have Office installed, the corresponding provider has the same bitness of Office, so with Office 64bit comes the ACE in 64bit version and the same for 32bit. This scenario poses a very difficult problem in deployment of your application. You need to have you application compiled with the same bitness or you will not be able to use the provider installed. (Or disinstall the incompatible Office version and reinstall the compatible one. Of course if you should be able to persuade your customer that Office is the incompatible app :-)

这篇关于如何打开在C#与Microsoft Access数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆