I. Introduction of the problem
A long time did not use C # to connect the database program, but found some information on the internet, basically still written 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 can not be found, or remote connection failure, the display error is error40.
The error is as follows:
Ii. Introduction of the environment
1, my database is sqlserver2008, the development environment used is vs2013,.net4.0
2. My database installation takes a named instance, so the server name should be "computer name \ Data instance name" When writing the database connection string
The database connection characters are:
When I found the connection error, I thought it was my sql2008 and vs2013 the wrong environment caused by the error, I will open the previous project, attach the database, change the data connection string, found that the error is the same.
Third, error resolution
I've been looking for one hours. Error finally found that the previous write SQL connection character before the previous will add an @ symbol, because I have the character string "\", if not added will prompt error, so I will add every time. But the last time the system was re-installed, the named instance was changed to the name beginning with the "T" character. Then "\ t" This expression is a table character, so it is not an error, itself is an escape symbol of the correct use of the occasion, because there is no error, also forgot to add "@" .
The solution is to precede the string with an @.
Attention
Write the SQL connection string, or the Windows platform to write the file path when you remember to add the @ character, to form a good habit, let the small trap go to hell.
A small trap about the character "\" escape