0

My code is as follows:

string constring = "Data Source=132.186.127.169"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";
SqlConnection con;
con = new SqlConnection(constring);
con.Open();
string query="SELECT * from CadPoolProjectTable1";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
MessageBox.Show("selected");
con.Close(); 

I am getting error at the line con.Open();. The error is:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

0

6 Answers 6

4

You are missing a ';' after the server name in the connection string.

string constring = "Data Source=132.186.127.169"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";

It should be

string constring = "Data Source=132.186.127.169;"+ "Initial Catalog=CadPool1;" + "Integrated Security=True";

The error says that your app was not able to connect to the server. I would do the following.

  1. Check for the server address (on a quick look it looks good)
  2. Connect using management studio and in your case it should have worked.

It means the issue is with the code. Since you are concatenating the string I would debug the code and see what the end result for the connection string is.

Tip:If it is a web application add the connection string to web.config file. More info here How to: Read Connection Strings from the Web.config File

Sign up to request clarification or add additional context in comments.

1 Comment

Hah I got the answer in first but you got the votes? :) Oh well, maybe next time ...
3

You're missing a semicolon in your connection string

Data Source=132.186.127.169;"+ "Initial...
                           ^

If you need to build the connection string yourself you can use the SqlConnectionStringBuilder class. That way you won't to be as troubled by these subtle mistakes.

1 Comment

+1 for using the ConnectionStringBuilder instead of concatenating together everything manually...
2

Your connection string is wrong:

string constring = 
    "Data Source=132.186.127.169;Initial Catalog=CadPool1;Integrated Security=True";

You don't need to concatenate the strings together, but more importantly, you were missing the semi-colon ";" between the data source and the initial catalog settings.

Comments

0

First, you are missing a ; (semicolon) between Data Source and Initial Catalog.

Second, if this is a newly installed SQL Server instance, you may need to go into SQL Server Configuration Manager, and enable the protocol(s) you'll need.

Comments

0

Please check if you can ping the server mentioned via cmd Also try telnet to the server from your machine. One more thing to check would be port the server is configured for if its not the default one you will have to add the port as 132.186.127.169,XXX

1 Comment

+1 To offset the downvote; this is sound advise, and if you copy/paste the servername from the watch inspector, you'd notice it included the dbname :)
0

Servername in the connection string is wrong. and also since there are no dynamic values you don't need string concatenation. Change it to:

string constring = "Data Source=132.186.127.169;
                    Initial Catalog=CadPool1;
                    Integrated Security=True";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.