与sql server建立连接时出错 [英] error while establishing connection with sql server

查看:89
本文介绍了与sql server建立连接时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到sqlServer.为此,我正在使用以下代码:

I am trying to connect to sqlServer. For this I am using following code:

string server = "1.1.1.1,1433";
var stringBuilder = new SqlConnectionStringBuilder
                    {
                        UserID = "id",
                        Password = "pass",
                        MultipleActiveResultSets = true,
                        DataSource = server,
                        NetworkLibrary = "DBMSSOCN",
                        InitialCatalog = "iCatalog"
                    };


using (var con = new SqlConnection(stringBuilder.ToString()))
{
    try
    {
        con.Open();
        MessageBox.Show("Connected");
    }
    catch (Exception exception)
    {
        MessageBox.Show(exception.Message);
    }
}


但是,当程序尝试打开连接时,出现异常与sql Server建立连接时发生了与网络相关或特定于实例的错误....".如果我将ipAddress和port更改为serverName和实例,如下所示:


But when programm trying to open connection I get exception "A network-related or instance-specific error occurred while establishing connection to sql Server....". And if I change ipAddress and port to serverName and instance like this:

var server = @"MSSQL01\SQLENTW";


我能够连接到服务器. ConnectionString类似于connectionstrings.com中的内容.另外我可以ping IP地址和端口,默认情况下是Sql Server中的1433.我尝试像这里这样建立连接


I am able to connect to server. ConnectionString is like in connectionstrings.com. Also I can ping ip address and port by default is 1433 in Sql Server. I tried make connection like here

string server = "1.1.1.1\SQLENTW,1433"; 
or 
string server = "1.1.1.1:1433";



那么有人可以指出我做错了什么吗?

在此先感谢.



So can anybody point me, where I am doing wrong?

Thanks in advance.

推荐答案

string server = "1.1.1.1,1433";


错了格式为IPAddress:Port而不是IPAddress,Port.可能1.1.1.1不是服务器的正确IP地址.


That''s wrong. The format is IPAddress:Port not IPAddress,Port. And likely 1.1.1.1 is not the correct IP address of the server.


请尝试用服务器名称替换IP.如果仍然存在问题,
然后确保已启用TCP/IP并允许允许远程连接".

要启用TCP/IP,请遵循以下步骤:
1.打开Sql Server配置管理器
2.在左侧面板中,单击"SQL Sever网络配置"
3.然后进一步单击"MSSQLSERVER节点的协议".
4.在右侧面板上,您将找到TCP/IP,右键单击它,然后仅启用.

要允许在SQL SERVER上进行远程连接,请遵循以下步骤:

1.打开通过Windows或SQL身份验证连接的MS SQL SERVER.
2.右键单击服务器",然后转到属性.
3.在左窗格中,单击连接节点".
4.在右侧窗格中,您会看到允许远程连接到该服务器的复选框"复选框,并选中复选框".单击确定.
5.再次右键单击Server并重新启动SQL Server.
Once try by replacing IP with Server Name. If still problem exist,
then make sure TCP/IP is enabled and Allow Remote Connection are allowed.

To Enable TCP/IP go trough following steps:
1. Open Sql Server Configuration Manager
2. In left panel click SQL Sever Network Configuration
3. Then further Click on Protocols for MSSQLSERVER Node.
4. On the right panel you will find TCP/IP, Right Click it and just Enable.

To Allow Remote Connection on SQL SERVER go trough following steps:

1. Open MS SQL SERVER connect with Windows or SQL Authentication.
2. Right click on Server and go to properties.
3. In Left Pane click on Connection Node.
4. On Right Pane, you will find CheckBox saying Allow Remote Connection to this Server, Check this box & Click Ok.
5. Again right click on Server and Restart SQL Server.


这篇关于与sql server建立连接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆