如果 Ole.DB 提供程序在系统上可用,为什么 OleDbConnection.Open() 会抛出无法识别的数据库格式 [英] Why OleDbConnection.Open() throws Unrecognized database format if Ole.DB provider is available on the system

查看:39
本文介绍了如果 Ole.DB 提供程序在系统上可用,为什么 OleDbConnection.Open() 会抛出无法识别的数据库格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows x64 上尝试了下一个代码,但代码被编译并作为 x86 运行.如果我在 Windows 7 或 10 x86 中运行应用程序,则行为相同.

static IListGetOleDbProviders(){OleDbEnumerator oleDbEnumerator = new OleDbEnumerator();数据表 oleDbProviders = oleDbEnumerator.GetElements();IDictionary<字符串,字符串>descriptions = new Dictionary();int typeColumnIndex = oleDbProviders.Columns.IndexOf("SOURCES_TYPE");int nameColumnIndex = oleDbProviders.Columns.IndexOf("SOURCES_NAME");int descriptionColumnIndex = oleDbProviders.Columns.IndexOf("SOURCES_DESCRIPTION");foreach(oleDbProviders.Rows 中的 DataRow 数据行){int type = (int)dataRow[typeColumnIndex];如果(类型== 1){string name = (string)dataRow[nameColumnIndex];string description = (string)dataRow[descriptionColumnIndex];描述.添加(名称,描述);}}IList<字符串>provider = new List();foreach(描述中的 KeyValuePair 对){提供者.添加(对.值);}返回提供者;}静态无效测试5(){//有项目 'Microsoft.Jet.Ole.DB.4.0'IList<字符串>提供者 = GetOleDbProviders();string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\my.accdb";System.Data.Common.DbConnection connection = new OleDbConnection(connectionString);尝试{//在 32 位上抛出 OleDbException 并带有消息Unregonized database format"连接.打开();}catch (InvalidOperationException e){//在 64 位运行时运行时的断点}捕获 (OleDbException e){//在 32 位运行时运行时的断点}}

如果Jet.OleDb 是系统提供的,connection.Open() 为什么会抛出异常?或者这是否意味着 Jet.OleDb 支持其他格式但不支持 *.accdb.

当然在安装

编辑

安装 Microsoft Access 2013 Runtime 后仍然不起作用.您必须使用更新的 ACE 提供程序 而不是 Jet.必须相应地更新连接字符串.

解决方案

或者这是否意味着 Jet.OleDb 支持其他格式但不支持*.accdb.

是 - 较旧版本的驱动程序将支持较旧的 mdb 格式.

根据 文档:

<块引用>

从 Access 2007 开始,.accdb 是默认的 Access 文件格式.

在 Access 2007 中引入 .accdb 文件格式之前,Access文件格式使用 .mdb 文件扩展名.

如果您有兴趣,该链接将为您提供有关 mdb 和 accdb 之间差异的更多信息.

<块引用>

如果 Microsoft.Jet.Ole.Db.4.0 提供程序不会更正确从 oleDbEnumerator.GetElements() 和 ProviderNotFound 返回会抛出异常?

不,不会的.提供者.它不支持所有版本的 Access 数据库并不意味着它不存在.如果它根本不可见,人们将无法使用该驱动程序访问 mdb 文件 - 例如(破坏了许多旧的 VB6 应用程序).

I tried next code on Windows x64, but the code is compiled and run as x86. The same behaviour is if I run the application in Windows 7 or 10 x86.

static IList<string> GetOleDbProviders()
{
    OleDbEnumerator oleDbEnumerator = new OleDbEnumerator();
    DataTable oleDbProviders = oleDbEnumerator.GetElements();

    IDictionary<string, string> descriptions = new Dictionary<string, string>();
    int typeColumnIndex = oleDbProviders.Columns.IndexOf("SOURCES_TYPE");
    int nameColumnIndex = oleDbProviders.Columns.IndexOf("SOURCES_NAME");
    int descriptionColumnIndex = oleDbProviders.Columns.IndexOf("SOURCES_DESCRIPTION");

    foreach (DataRow dataRow in oleDbProviders.Rows)
    {
        int type = (int)dataRow[typeColumnIndex];

        if (type == 1)
        {
            string name = (string)dataRow[nameColumnIndex];
            string description = (string)dataRow[descriptionColumnIndex];
            descriptions.Add(name, description);
        }
    }

    IList<string> providers = new List<string>();

    foreach (KeyValuePair<string, string> pair in descriptions)
    {
        providers.Add(pair.Value);
    }

    return providers;
}

static void Test5()
{
    // has item 'Microsoft.Jet.Ole.DB.4.0'
    IList<string> providers = GetOleDbProviders();

    string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\my.accdb";
    System.Data.Common.DbConnection connection = new OleDbConnection(connectionString);

    try
    {
        // throws OleDbException on 32 bit with message 'Unregonized database format'
        connection.Open();
    }
    catch (InvalidOperationException e)
    {
        // break point when running on 64-bit runtime
    }
    catch (OleDbException e)
    {
        // break point when running on 32-bit runtime
    }
}

Why would connection.Open() throw an exception if the Jet.OleDb is provided by the system? Or does this mean that Jet.OleDb has support for other formats but not *.accdb.

Of course after installing Microsoft Access 2013 Runtime it works, but still? Wouldn't be more correct if Microsoft.Jet.Ole.Db.4.0 provider won't be returned from oleDbEnumerator.GetElements() and ProviderNotFound exception would be thrown?

EDIT

After installing Microsoft Access 2013 Runtime it still does not work. You have to use newer ACE provider instead of Jet. Connection string must be updated accordingly.

解决方案

Or does this mean that Jet.OleDb has support for other formats but not *.accdb.

Yes - the older versions of the driver will support the older mdb format.

As per the docs:

Starting with Access 2007, .accdb is the default Access file format.

Before the .accdb file format was introduced in Access 2007, Access file formats used the .mdb file extension.

That link will give you more information about the differences between mdb and accdb if you are interested.

Wouldn't be more correct if Microsoft.Jet.Ole.Db.4.0 provider won't be returned from oleDbEnumerator.GetElements() and ProviderNotFound exception would be thrown?

No, it wouldn't be. The provider is there. The fact it doesn't support all versions of Access databases doesn't mean it doesn't exist. If it wasn't visible at all, people wouldn't be able to use the driver to access mdb files - as an example (breaking lots of old VB6 apps).

这篇关于如果 Ole.DB 提供程序在系统上可用,为什么 OleDbConnection.Open() 会抛出无法识别的数据库格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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