2013-03-26 176 views
0

我在本地系统上安装了SQL Server 2008安装程序,而在.NET 4.5中安装了C#中的以下代码。本地无法连接到SQL Server

private Boolean ConnectDatabase() 
    { 
     if(!this.IsInitialized()) 
     { 
      return false; 
     } 
     else 
     { 
      try 
      { 
       String ConnectionString = "User ID=" + this.DatabaseUsername + ";" + 
              "Password=" + this.DatabasePassword + ";" + 
              "Server=" + this.DatabaseHost + "," + this.DatabasePort + ";" + 
              "Database=" + this.DatabaseResource + ";"; 
       this.DatabaseConnection = new SqlConnection(ConnectionString); 
       this.DatabaseConnection.Open(); 
       return true; 
      } 
      catch (Exception e) 
      { 
       return false; 
      } 
     } 
    } 

我可以连接到SSMS中的服务器,但使用相同的凭据我无法在C#中获得连接。

连接字符串如下:

User ID=sa;Password=******;Server=CPUNAME\MSSQL,1433;Database=DBNAME; 

CPUNameDBName,和**与它们的相应的值替换。我也尝试过使用Windows Credentials和各种不同的服务器端点,包括我的NB名称,Loopback IP,静态内部IP,等等等等。防火墙关闭。 TCP在服务器上启用。

我得到以下异常:

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: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

有什么建议?

+1

是sqlexpress数据库吗? – 2013-03-26 16:22:19

+0

否定的。 SQL Server企业版。服务正在运行。 – DigitalJedi805 2013-03-26 16:28:29

+0

我怀疑你的SERVER部分不正确。试用(本地)或只是你的电脑的名字。 – Steve 2013-03-26 16:29:17

回答

1

您是否在SQL Server配置管理器中启用了协议?

参见Enable TCP/IP Network Protocol for SQL Server

[编辑]
没关系;我错过了“服务器上启用了TCP”部分;-)
然后问题就变成了:您是否在启用TCP/IP后重启 SQL服务器?您需要重新启动SQL服务(see step 6)以使这些更改生效。

还要验证您使用的是正确的实例名称(您当前使用MSSQL作为实例名称,可能是SQLExpress或其他)。

+0

正确的实例名称,计算机已重启四次。 – DigitalJedi805 2013-03-26 16:28:48

+0

并且CPUName解析为正确的主机? (例如,尝试'ping CPUName')。港口也验证实际上是1433?你也可以看看[这篇文章](http://blogs.msdn.com/b/spike/archive/2008/12/19/provider-tcp-provider-error-0-no-connection-可待定制的,因为最target机器积极-拒绝-it.aspx);它提到可能存在“别名”,导致错误的主机得到解决。 – RobIII 2013-03-26 16:30:00

+0

我在配置管理器中验证了端口被设置为默认值1433.我也可以向你保证CPUName可以解析。 – DigitalJedi805 2013-03-26 17:14:55

1

有很多服务必须运行才能连接,即使在本地。其中一些(有用)默认设置为“手动启动”。您可能想要检查的服务包括SQL Server,SQL Server代理和分布式事务处理协调器。

+0

所有SQL相关服务以及DTC都在运行。 – DigitalJedi805 2013-03-26 17:16:06

相关问题