0

This line of code

DbProviderFactory factory = DbProviderFactories.GetFactory("Microsoft.SqlServerCe.Client");

throws this exception

System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.

Although my machine.config file contains the following section

<system.data>
  <DbProviderFactories>
    <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <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" />
    <add name="SQL Server CE Data Provider" invariant="Microsoft.SqlServerCe.Client" description=".NET Framework Data Provider for Microsoft SQL Server 2005 Compact Edition" type="Microsoft.SqlServerCe.Client.SqlCeClientFactory, Microsoft.SqlServerCe.Client, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  </DbProviderFactories>
</system.data>

Am I missing something? Should I look somewhere else? Code like DbProviderFactory factory = SqlCeProviderFactory.Instance works fine.

Joseph Idziorek
  • 4,853
  • 6
  • 23
  • 37
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
  • I don't have any of the DLLs listed in Microsoft.* in the machine.config section for Sql Server Ce... I have System.Data.SqlServerCe v9.0.242.0. – Anthony Mastrean May 05 '09 at 14:03

2 Answers2

2

There are multiple SQL Server CE installs. The Visual Studio tools provides the server explorer and design components and places the SQL Server CE DLLs in a location local to Visual Studio. I needed to install the runtime to get the data provider registered in the machine.config file.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
1

In Visual Studio 2012, the default ProviderName is System.Data.SqlServerCe.4.0.

This lets me find a factory by name:

String providerName = "System.Data.SqlServerCe.4.0";

DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
if (factory == null)
   throw new Exception("Unable to locate factory for " + providerName);

DbConnection conn = factory.CreateConnection();
conn.ConnectionString = connectionString;
conn.Open();

Note: Any code is released into the public domain. No attribution required.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219