NHibernate连接字符串:如何指定端口号和server \ instance?

时间:2009-07-27 06:04:34

标签: nhibernate configuration database-connection

我用NHibernate 2.1替换旧的DAL。我的NHibernate配置在我的本地开发机器上运行,但在UAT上运行。 UAT数据库是非默认端口上的群集设置。我正在使用类似于下面的标准NHibernate confie文件:

<?xml version="1.0" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>
    </session-factory>
</hibernate-configuration>

我认为问题是我在NHibernate配置文件中指定连接字符串的方式,因为我现有的DAL使用以下连接字符串:

Data Source=uatserver\db01,1433;
        Initial Catalog=dbname;
        User ID=dbuser;
        Password=userpassword

在NHibernate配置文件中,我尝试了以下组合,没有一个工作,我收到了不同的错误消息,但大多数人都说无法连接。

<property name="connection.connection_string">
        Server=tcp:(uatserver\db01),1433;
        Initial Catalog=dbname;
        User ID=dbuser;
        Password=userpassword</property>

错误:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:TCP提供者,错误:0 - 没有这样的主机。)

<property name="connection.connection_string">
        Server=(uatserver\db01),1433;
        Initial Catalog=dbname;
        User ID=dbuser;
        Password=userpassword</property>

<property name="connection.connection_string">
        Server=uatserver\db01,1433;
        Initial Catalog=dbname;
        User ID=dbuser;
        Password=userpassword</property>

<property name="connection.connection_string">
        Server=(uatserver\db01, 1433);
        Initial Catalog=dbname;
        User ID=dbuser;
        Password=userpassword</property>

这是日志中的最后一行:

[27 Jul 2009 18:27] NHibernate.Connection.DriverConnectionProvider
        [DEBUG] Obtaining IDbConnection from Driver

2 个答案:

答案 0 :(得分:5)

尝试将服务器部分更改为:

Server=tcp:(local),12345

(或任何端口号)。您只能为TCP / IP连接指定端口。

答案 1 :(得分:1)

尝试在单独的属性上传递端口,如下所示服务器= 127.0.0.1;端口= 3306

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
  <property name="connection.connection_string">Server=127.0.0.1;Port=3306;Database=test;User ID=admin;Password=admin</property>
  <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
  <property name="hbm2ddl.auto">update</property>
  <property name="current_session_context_class">web</property>
  <mapping assembly="WebAppTest" />
</session-factory>
</hibernate-configuration>