开发者

Error trying to connect to SQL Server using C#

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 re开发者_运维问答mote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)


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


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.


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.


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.


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


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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜