2012-05-02 32 views
0

我在的app.config以下設置:錯誤打開SQL Server精簡版4.0使用NHibernate

<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) 

這是我一樣的錯誤T當我改變連接屬性所推薦的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服務器的運行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位,我正在使用生成後事件將數據庫和DLL複製到應用程序文件夾。

有什麼我失蹤或做錯了嗎?

回答

3

我發現我做錯了。以下屬性<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>適用於SQL Server,不適用於SQL Server Compact Edition。用NHibernate.Driver.SqlServerCeDriver替換屬性值可以正常工作。

相關問題