简体   繁体   English

NHibernate没有连接到sql server

[英]NHibernate is not connecting to sql server

When i set up a regular connection, it works, however when i try to use nhibernate, hibernate.cfg.xml, im getting the following error. 当我设置常规连接时,它工作,但是当我尝试使用nhibernate,hibernate.cfg.xml时,我得到以下错误。

Message="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)" Source=".Net SqlClient Data Provider" Message =“建立与SQL Server的连接时发生与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。验证实例名称是否正确以及SQL Server是否配置为允许远程连接。” provider:SQL网络接口,错误:26 - 找到指定的服务器/实例时出错)“Source =”。Net SqlClient Data Provider“

What would be the reason for this and how can i resolve it ? 这是什么原因,我该如何解决?

I doubt that it sa network or sql server configuration error. 我怀疑它是网络或SQL服务器配置错误。

<?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=(ServerName\DEV_ENV);Initial Catalog=dbName;User Id=SA;Password=PASS</property>

    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

  </session-factory>
</hibernate-configuration>

Is your connection string correct? 你的连接字符串是否正确?

http://www.connectionstrings.com/sql-server-2005 http://www.connectionstrings.com/sql-server-2005

Try to connect to the server with the same connection string, but with another data access technology, for example pure ADO.NET. 尝试使用相同的连接字符串连接到服务器,但使用其他数据访问技术,例如纯ADO.NET。

If that fails as well, then it's not a NHibernate problem. 如果那也失败了,那么它不是NHibernate的问题。
Maybe your connection string or some SQL server settings are not correct, as indicated in the error message: 可能您的连接字符串或某些SQL服务器设置不正确,如错误消息中所示:

"Verify that the instance name is correct and that SQL Server is configured to allow remote connections." “验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。”

This is the format i have used for over a dozen applications for SQL SERVER 2005/2008 这是我用于SQL SERVER 2005/2008的十几个应用程序的格式

data source=COMPUTERNAME;User Id=USERNAME;Password=PASSWORD;database=DATABASENAME

or if you require an instance name 或者如果您需要实例名称

data source=COMPUTERNAME\SERVERINSTANCE;User Id=USERNAME;Password=PASSWORD;database=DATABASENAME

replace the placeholders in capital letters. 用大写字母替换占位符。 Also ensure that you have set up sql-server to allow connections for Sql Server Authentication and not only Windows Authentication . 还要确保已设置sql-server以允许连接Sql Server Authentication ,而不仅仅是Windows Authentication Check your firewall as well 检查防火墙

I wrote a small piece of code in the same application to open a raw ado.net connection with the same connection string and it worked. 我在同一个应用程序中编写了一小段代码,用相同的连接字符串打开原始的ado.net连接,并且它有效。

Then and I copy pasted that connection string into the configuration file for nhibernate and it started working. 然后我将该连接字符串粘贴到nhibernate的配置文件中,并开始工作。

When I compared the original string giving the error and the new connection string... I couldn't see any difference whatsoever... no clue what's going on... totally stumped... anyways the problem's solved... hope this helps someone. 当我比较给出错误的原始字符串和新的连接字符串时...我看不出任何差别......不知道发生了什么......完全难倒...无论如何问题已经解决了...希望这个帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM