如何将SQL Server CE与Enterprise Library 5 DatabaseFactory集成?

时间:2012-02-29 02:20:29

标签: c# sql-server-ce-4 enterprise-library-5

我正在接受

  

“尝试获取类型实例时发生激活错误   数据库,键“”“

每当我将默认数据库引用到SQL Server CE

时,我的DatabaseFactory.CreateDatabase()会出现

异常

我尝试检查问题是否只是我的.dll参考,我证明这是错误的,因为我仍然可以连接到真正的SQL Server 2008数据库。

这是我的配置:

<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>
    </configSections>
<dataConfiguration defaultDatabase="sqlCEDB"/>
    <connectionStrings>
        <add name="sqlCEDB"
           connectionString="Data Source=|DataDirectory|\sqlCEDB.sdf;Password='somepassword'"
           providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>

以下是我创建数据库实例的方法:

private Database db;

protected internal Database DB
{
   get 
   {
      if (db == null)
         db = DatabaseFactory.CreateDatabase();
      return db; 
   }
}

更改配置属性 已解决:providerName =“System.Data.SqlServerCe.3.5”

1 个答案:

答案 0 :(得分:2)

<强>解

更改配置属性已解决:providerName =“System.Data.SqlServerCe.3.5”

<configSections>
    <section name="dataConfiguration" 
             type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
             requirePermission="false"/>
</configSections>
<dataConfiguration defaultDatabase="sqlCEDB"/>
    <connectionStrings>
        <add name="sqlCEDB"
             connectionString="Data Source=|DataDirectory|\sqlCEDB.sdf;Password='somepassword'"
             providerName="System.Data.SqlServerCe.3.5" />
    </connectionStrings>