Click here to Skip to main content
15,936,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to connect to sqlServer. For this I am using following code:

C#
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);
    }
}

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:
C#
var server = @"MSSQL01\SQLENTW";

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

C#
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.
Posted
Comments
ZurdoDev 9-Oct-13 9:57am    
I'm confused. You say that if you use the server name and instance name it does work? So, what's the issue?
Alexander Dymshyts 9-Oct-13 9:59am    
I need to get port and ip address from user and then establish connection with sql server
ZurdoDev 9-Oct-13 10:02am    
OK. So what's the question? Ask them for it, then use it. I'm not sure where you need help.
Alexander Dymshyts 9-Oct-13 10:17am    
The problem is, that I cannot open con.Open() when I get ipaddress and port from user. It give's me error :
Exception:Caught: "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 such host is known.)" (System.Data.SqlClient.SqlException)
ZurdoDev 9-Oct-13 10:30am    
Then that IP is not right. They have to provide valid connection information.

C#
string server = "1.1.1.1,1433";

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.
 
Share this answer
 
Comments
Alexander Dymshyts 10-Oct-13 9:49am    
I know that 1.1.1.1 is not the correct ip address. It's for example. Also according to this link http://www.connectionstrings.com/sql-server/ , server isn't wrong and I can use this format. Anyway even with format ipAddress:Port it doesn't work.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900