我正在用 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
最佳答案
尝试将服务器部分更改为:
Server=tcp:(local),12345
(或任何端口号)。您只能为 TCP/IP 连接指定端口。
关于NHibernate 连接字符串 : how to specify port number and server\instance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1186623/