简体   繁体   中英

Why OleDbConnection.Open() throws Unrecognized database format if Ole.DB provider is available on the system

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).

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