How to create DbProviderFactory object for SQL Server CE?

风格不统一 提交于 2020-07-24 03:16:36

问题


I'm trying to create an object for DbProviderFactory using the following code,

 try
 {
     var sql = ConfigurationManager.ConnectionStrings["sql"];
     DbProviderFactory sqlfactory = DbProviderFactories.GetFactory ( sql.ProviderName ); // This one works fine

     var sqlCE = ConfigurationManager.ConnectionStrings["sqlCE"];
     DbProviderFactory sqlCEfactory = DbProviderFactories.GetFactory ( sqlCE.ProviderName ); // This doesnt 
  }
  catch ( Exception ex )
  {
      Console.WriteLine ( ex.Message );
  }

and app.config:

<connectionStrings>
   <clear/>
   <add name="sql" 
        providerName="System.Data.SqlClient" 
        connectionString="Server=.\SQLExpress;Database=TestResults;Trusted_Connection=Yes;"/>
   <add name="sqlCE" 
        providerName="System.Data.SqlServerCe" 
        connectionString="DATA SOURCE=DataBase\dbTestResults.sdf"/>  
</connectionStrings>

The first one (sqlFactory) creates successfully, but the second object (sqlCEfactory) throws an exception:

enter image description here

Am I missing something?

Thanks!


回答1:


You have the wrong provider names specified in your config file.

The SQL Server CE invariant provider names are

System.Data.SqlServerCe.4.0 

(for version 4.0 runtime)

System.Data.SqlServerCe.3.5 

(for version 3.5 runtime)



来源:https://stackoverflow.com/questions/26488414/how-to-create-dbproviderfactory-object-for-sql-server-ce

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