我在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.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连接对话框中连接并测试连接。我已阅读并重新阅读private deployment vs. central deployment (SQL Server Compact)。对上述错误的任何搜索都会返回与我的问题无关的结果。
我使用的笔记本电脑运行的是Windows 7 Professional 64 Bit,我正在使用post-build事件将数据库和DLL复制到应用程序文件夹。
我有什么遗漏或做错了吗?
答案 0 :(得分:3)
我发现我做错了什么。以下属性<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
适用于SQL Server而不适用于SQL Server Compact Edition。用NHibernate.Driver.SqlServerCeDriver
替换属性值可以正常工作。