日期:2014-05-18  浏览次数:20759 次

使用SqlConnection进行远程连接,必须打开本地MSSERVER服务才能启动,啥原因
C# code

String strConnection = "uid=" + name.Trim() + ";pwd=" + password.Trim() + ";Server="+server.Trim()+","+port+";Database="+db.Trim();
SqlConnection conn = new SqlConnection(strConnection);


Server的值为远程IP,本地MSSQLSERVER没开启时,连接报错:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) 

但是我把本地MSSQLSERVER开启,就能成功连接了。
为什么啊,我连接的是远程数据库,不是本地的啊

------解决方案--------------------
那你把远程的数据库关掉,仅仅把本地的打开得了。
------解决方案--------------------
呵呵,是挺奇怪的。
------解决方案--------------------
设置问题吧 
this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. //这个说的是你默认设置有问题
请确认远程服务器打开
------解决方案--------------------
检查server property中security中是否启用sql和windows混和登录和connection中是否allow remote connection

检查configuration manager中是否启用了tcp ip和named pipe
也可以在surface area 检查configuration中检查database engine的remote connection设置

检查操作系统防火墙设置,打开1433端口

在configuration manager中tcp/ip的property中IP Address:IP1设置的IP和port均没错,是否在IPALL中设置了Dynamic Port
,请设置为1433
------解决方案--------------------
2个端口通信
------解决方案--------------------
你看看"Server="+server.Trim()中的server.Trim()的值是不是空,如果是空,则不会连接远程,而连接到本机.
------解决方案--------------------
有点怪
------解决方案--------------------
你打印出strConnection看什么
------解决方案--------------------
可能是目标计算机防火墙的问题,关掉防火墙看看能不能连接,如果能连接了,照里面的设置就行了
http://user.qzone.qq.com/740156554/blog/1267845610