OleDbConnection.Open()仅在一个项目中引发异常,相同的代码在其他项目中有效 [英] OleDbConnection.Open() throws exception only in one project, same code works in other projects

查看:503
本文介绍了OleDbConnection.Open()仅在一个项目中引发异常,相同的代码在其他项目中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

我知道要安装的通用修补程序:

Microsoft Access数据库引擎2010可再发行

2007 Office system驱动程序:数据连接组件

两者都安装在我的本地PC上.这是我的代码

        OleDbConnection conn = new OleDbConnection();
        string fileName = "test.xlsx";


        try
        {
            string connectString = String.Format(
                "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR={1};'",
                fileName, "YES");

            conn.ConnectionString = connectString;
            conn.Open(); //exception is thrown here !!!

            OleDbCommand comm = new OleDbCommand();
            comm.CommandText =
                string.Format("CREATE TABLE [{0}] ", "Test");

            comm.Connection = conn;
            comm.ExecuteNonQuery();

            OleDbDataAdapter ad = new OleDbDataAdapter(
                string.Format("SELECT * FROM [{0}]", "Test"), conn);
            OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }

我在本地计算机上的其他项目中尝试了此代码,一切正常.我有一个可以创建excel出口的项目,但是我没有这个问题.

问题出在项目中,我不知道如何解决.同样在当前项目上,我创建一个新的.aspx页,并在Page_Load中仅放置此代码,相同的异常.

其他信息:此项目写于vs 2008,转换为2010,此后用于vs2012.到目前为止,一切都在该项目中进行.

JET连接字符串也是如此!

编辑

在回答 Joe 之后,我看到该项目在64bitProcess上运行:

bool test = Environment.Is64BitProcess; //return true.

我的驱动程序适用于32位.我现在可以做的,我可以更改环境过程.我已经安装了Office 2010 x32,所以无法为64位进程安装驱动程序.

解决方案

用于32位和64位应用程序的OLEDB驱动程序是不同的.

如果仅安装了32位驱动程序,则尝试使用该驱动程序的64位应用程序将出现此错误.同样,如果仅安装了64位版本,则尝试使用该版本的32位应用程序将收到此错误.

您说:

我在本地计算机上的其他项目中尝试使用此代码,一切正常

Ergo,必须至少正确安装两者之一.

要了解正在发生的事情,您可以在有效的应用程序和无效的应用程序中检查Environment.Is64BitProcess.这样可以告诉您缺少哪个版本.

然后下载并安装缺少的32位或64位版本:

http://www.microsoft.com/zh-我们/download/details.aspx?id=13255

您需要AccessDatabaseEngine.exe(32位)或AccessDatabaseEngine_64.exe(64位)

请注意,对于Office 2010版本,您可能需要将提供程序指定为"Microsoft.ACE.OLEDB.14.0"(对于Office 2007,则是12.0).

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

I know the common fix for this which is to install:

Microsoft Access Database Engine 2010 Redistributable

Or

2007 Office System Driver: Data Connectivity Components

Both are installed on my local PC. This is the code which I have

        OleDbConnection conn = new OleDbConnection();
        string fileName = "test.xlsx";


        try
        {
            string connectString = String.Format(
                "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR={1};'",
                fileName, "YES");

            conn.ConnectionString = connectString;
            conn.Open(); //exception is thrown here !!!

            OleDbCommand comm = new OleDbCommand();
            comm.CommandText =
                string.Format("CREATE TABLE [{0}] ", "Test");

            comm.Connection = conn;
            comm.ExecuteNonQuery();

            OleDbDataAdapter ad = new OleDbDataAdapter(
                string.Format("SELECT * FROM [{0}]", "Test"), conn);
            OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }

I try this code in other projects on my local machine and everything is working. I have project which creates excel exports and I don't have this problem.

The problem is in the project I don't understand how to fix it. Also on the current project I create one new .aspx page and in Page_Load put only this code, same exception.

Additional information: this project was written on vs 2008, convert to 2010 and after that used in vs 2012. Everything was working in the project till now.

Also same thing for JET connection string !

EDIT

After the answer of Joe I see that this project is run on 64bitProcess:

bool test = Environment.Is64BitProcess; //return true.

My driver is for 32bit. What I can do now, can I change the environment process. I can't install the driver for 64bit process because I have installed Office 2010 x32.

解决方案

The OLEDB drivers for 32-bit and 64-bit applications are different.

If you only have the 32-bit driver installed, then 64-bit applications that attempt to use it will get this error. Similarly, if you have only the 64-bit version installed, then 32-bit applications that attempt to use it will get this error.

You say:

I try this code in other projects on my local machine and everything is working

Ergo, at least one of the two must be correctly installed.

To understand what's happening you could examine Environment.Is64BitProcess in both the application that works, and the one that doesn't. That will tell you which version is missing.

Then download and install the 32-bit or 64-bit version that's missing from:

http://www.microsoft.com/en-us/download/details.aspx?id=13255

You need AccessDatabaseEngine.exe (32-bit) or AccessDatabaseEngine_64.exe (64-bit)

Note that you may need to specify the provider as 'Microsoft.ACE.OLEDB.14.0' for the Office 2010 version (12.0 was for Office 2007).

这篇关于OleDbConnection.Open()仅在一个项目中引发异常,相同的代码在其他项目中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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