-1

我在 app.config 中有以下设置:

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="Nh.Data">
       <property name="connection.provider">
           NHibernate.Connection.DriverConnectionProvider
       </property>
       <property name="dialect">
           NHibernate.Dialect.MsSqlCeDialect  </property>
       <property name="connection.driver_class">
           NHibernate.Driver.SqlClientDriver
       </property>
       <property name="connection.connection_string">
           Data Source=NhData.sdf
       </property>
       <property name="adonet.batch_size">16</property>
       <property name="generate_statistics">true</property>
       <property name="show_sql">true</property>
       <mapping assembly="Nh.Model"/>
    </session-factory>
</hibernate-configuration>

使用以下代码访问数据库时,

    private ISessionFactory CreateSessionFactory(){
        var cfg = new NHibernate.Cfg.Configuration().Configure();
        return cfg.BuildSessionFactory();
    }

我在线上收到以下错误return cfg.BuildSessionFactory();

    A network-related or instance-specific error occurred while
    establishing a connection to SQL Server. The server was not found or
    was not accessible. Verify that the instance name is correct and
    that SQL Server is configured to allow remote connections.
    (provider: Named Pipes Provider, error: 40 - Could not open a
    connection to SQL Server)

这与我按照Connection Strings.com的建议更改连接属性时遇到的错误相同

当我将connection.connection_string属性更改为 时Data Source=|DataDirectory|\bin\Debug\NhData.sdf,错误更改为

    A network-related or instance-specific error occurred while
    establishing a connection to SQL Server. The server was not found or
    was not accessible. Verify that the instance name is correct and
    that SQL Server is configured to allow remote connections.
    (provider: SQL Network Interfaces, error: 26 - Error Locating
    Server/Instance Specified)

我在应用程序文件夹中有以下文件以启用 SQL Server Compact 的运行

    sqlceca40.dll      sqlcecompact40.dll           sqlceoledb40.dll
    sqlceer40EN.dll    sqlceme40.dll                sqlceqp40.dll
    sqlcese40.dll      System.Data.SqlServerCe.dll  NhData.sdf

我知道数据库文件的路径是正确的,因为我可以从 Visual Studio IDE 连接对话框中连接和测试连接。我已经阅读并重新阅读了私有部署与中央部署(SQL Server Compact)。对上述错误的任何搜索都会返回与我的问题无关的结果。

我正在使用的笔记本电脑正在运行 Windows 7 Professional 64 位,我正在使用构建后事件将数据库和 DLL 复制到应用程序文件夹。

我有什么遗漏或做错了吗?

4

1 回答 1

3

我发现我做错了什么。以下属性<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>适用于 SQL Server 而不是 SQL Server Compact Edition。用工作正常替换属性值 NHibernate.Driver.SqlServerCeDriver

于 2012-05-04T06:21:21.577 回答