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

空扰寡人 提交于 2019-12-11 06:06:44

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/57112752/why-oledbconnection-open-throws-unrecognized-database-format-if-ole-db-provide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!