2013-02-23 121 views
8

如何使用SQL Server 2012为Nhibernate编写正确的连接字符串?错误的连接字符串NHibernate 3.3

我该写数据库名吗?

错误: 我得到错误与错误的“初始目录”

为NHibernate的不正确的连接字符串(我复制从我的服务器连接字符串):

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property> 
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property> 
    <property name="connection.connection_string">Data Source=RAFAL-KOMPUTER\MSSQLSERVER4;Initial Catalog=rafal;Integrated Security=True</property> 
    <property name="show_sql">true</property> 
    </session-factory> 
</hibernate-configuration> 

我从这个部分复制连接字符串: enter image description here

我想也是这个,但没有帮助。

<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> 
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
    <property name="connection.connection_string">Data Source=RAFAL-KOMPUTER\MSSQLSERVER4;Initial Catalog=rafal;Integrated Security=True</property> 

我`吨知道如何正确的为SQL Server 2012的

回答

7

第一个代码段不应该工作配置,而司机是CE(精简版)。

第二个看起来更好,甚至更多它为我工作。 (在这里看到更多http://www.connectionstrings.com/sql-server-2012)。最重要的是,要有正确的提供者名称设置(请点击此处:https://stackoverflow.com/a/8150792/315850)。试试这个调整片段(只是要确保所有部件都设置正确)

<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
<!-- to profit from features in 2012, use its dialect --> 
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property> 
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
<!-- the simplest connection string --> 
<property name="connection.connection_string">Data Source=RAFAL-KOMPUTER\MSSQLSERVER4;Database=rafal;Trusted_Connection=True;</property> 

我们必须确保正确的驱动程序使用(不符合CE或任何其他然后NHibernate.Driver.SqlClientDriver这意味着System.Data.SqlClient

双检查您1)SQL服务器和命名实例是:RAFAL-KOMPUTER\MSSQLSERVER4和2)数据库名称为:rafal 3)登录有权访问它,这必须努力

+0

现在我得到这个错误 - >而与SQL Server建立连接时出现与网络相关的或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,并将SQL Server配置为允许远程连接。 (提供程序:共享内存提供程序,错误:40 - 无法打开到SQL Server的连接) – 2013-02-24 09:26:56

+0

正如我告诉过你的第二个设置,我能够运行它!所以这个问题在别处。也许浏览器服务没有运行,也许TCP没有启用....换句话说。 NHibernate的配置现在正确(我会说),问题在别处。我建议,试着在这里搜索“与网络有关的......”。很快,你应该得到一些提示 – 2013-02-24 10:06:39

+0

http://stackoverflow.com/questions/1391503/a-network-related-or-instance-specific - 错误而出现的,而确立-A-连接到 – 2013-02-24 10:07:05

0

只需更换:SqlServerCeDriver通过SqlClientDriver如下:

替换:<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>

通过:<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>