Registering DbProviderFactories

Registering DbProviderFactories

Added by Nasir Mahmood updated on Wednesday, May 28, 2014

Problem:

There is common exception “Unable to find the requested .Net Framework Data Provider. It may not be installed” while working on new database factories like Sqlite or Mysql the reason behind this exception is no registration of class factory. According to MSDN “Each .NET Framework data provider that supports a factory-based class registers configuration information in the DbProviderFactories”.

Our task is Registering DbProviderFactories Using Configuration file Or using Programmatically.

Using Configuration Files:

Add the following code in .config file


<system.data>
  <DbProviderFactories>
    <add name="SqlClient Data Provider"
         invariant="System.Data.SqlClient"
         description=".Net Framework Data Provider for SqlServer"
         type="System.Data.SqlClient.SqlClientFactory, System.Data, 
              Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    />
  </DbProviderFactories>
</system.data>

Programmatically:

By following function we can register our factory class as

public bool RegisterDbProvider(string invariant, string description, string name, string type)
{
    try
    {
        DataSet ds = ConfigurationManager.GetSection("system.data") as DataSet;
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            if (row["InvariantName"].ToString() == invariant)
            {
                return true;
            }
        }
        ds.Tables[0].Rows.Add(name, description, invariant, type);
        return true;
    }
    catch
    {
    }
    return false;
}

Register Sqlite .Net Factory Provider:

Register Sqlite .Net Factory Provider

.Net data provider for sqllite can be found System.Data.SQLite

Add the following code in .config file


<system.data>
  <DbProviderFactories>
    <add name="SQLite Data Provider"
         invariant="System.Data.SQLite"
         description=".Net Framework Data Provider for SQLite"
         type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" 
    />
  </DbProviderFactories>
</system.data>

Or by using above function

RegisterDbProvider("System.Data.SQLite",".Net Framework Data Provider for SQLite",
                   "SQLite Data Provider","System.Data.SQLite.SQLiteFactory, System.Data.SQLite");

Register Oracle .Net Factory Provider:

Register Oracle .Net Factory Provider

.Net data provider for Mysql can be found Oracle Data Provider for .NET

Add the following code in .config file


<system.data>
  <DbProviderFactories>
    <add name="Oracle Data Provider for .NET"
         invariant="Oracle.DataAccess.Client"
         description="Oracle Data Provider for .NET"
         type="Oracle.DataAccess.Client.OracleClientFactory,
          Oracle.DataAccess,
          Version=2.112.3.0,
          Culture=neutral,
          PublicKeyToken=89b483f429c47342"
    />
  </DbProviderFactories>
</system.data>

Or by using above function

RegisterDbProvider("Oracle.DataAccess.Client","Oracle Data Provider for .NET",
                   "Oracle Data Provider for .NET",
                   "Oracle.DataAccess.Client.OracleClientFactory,Oracle.DataAccess
                   ,Version=2.112.3.0,Culture=neutral,PublicKeyToken=89b483f429c47342");

Register MySql .Net Factory Provider:

Register MySql .Net Factory Provider

.Net data provider for Mysql can be found Mysql Connector-Net

Add the following code in .config file


<system.data>
  <DbProviderFactories>
    <add name="MySQL Data Provider"
         invariant="MySql.Data.MySqlClient"
         description=".Net Framework Data Provider for MySQL"
         type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, PublicKeyToken=c5687fc88969c44d"
    />
  </DbProviderFactories>
</system.data>

Or by using above function

RegisterDbProvider("MySql.Data.MySqlClient",".Net Framework Data Provider for MySQL",
                   "MySQL Data Provider",
                   "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, PublicKeyToken=c5687fc88969c44d");                   

Related Tags

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected