TAGS :Viewed: 13 - Published at: a few seconds ago

[ Can we open two DB providers at a time? ]

Wondering if it is ever possible to open two database providers in a single application at a time?

I know the provider model, but that will help to provide one db provider initialization at one time. How can we make two/ multiple?

Or any other way?

Answer 1


Yes, this is possible here is an example:

 <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>

Then you can get the provider factory by name:

DbProviderFactory factory =
                DbProviderFactories.GetFactory(providerName);

            connection = factory.CreateConnection();

More information you can find here: https://msdn.microsoft.com/en-us/library/dd0w4a2z%28v=vs.110%29.aspx