A solution that shares the SQL database instance name caused by the character "\" Escape or fails the remote connection and displays an error ERROR40:
First, the problem introduction
A long time no use C # to connect the database program, however, found some information on the internet, basically still write out, but debugging the initial completion of the program, but found in Con.open () This method is always wrong, said that the database instance name could not be found, or the remote connection failed, the error displayed is Error40.
The error is as follows:
Second, the environment introduction
1, my database is sqlserver2008, using the development environment is vs2013,.net4.0
2, my database installation of the named instance, so when writing the database connection string, the server name should be "computer name \ Data Instance Name"
The database connection characters are:
When I found the connection error, I thought it was my sql2008 and vs2013 environment wrong, then opened the previous project, the database attached, change the data connection string, found that the error is the same.
Iii. Solutions
One-hour error found the last time you wrote a SQL connection character, you would add a @ symbol, because I have the character "\" in the string, and if not, I will add it every time. But the last time the system was reset, the named instance was changed to the name beginning with the "T" character. Then "\ t" this representation is a table character, so it is not an error, it is an escape symbol in the correct use of the situation, because there is no error, also forget to add "@".
The solution is to precede the string with a @.
Attention
When writing a SQL connection string, or when writing a file path on the Windows platform, remember to precede it with the @ character and develop good habits.