A small trap for escape character "\", character escape trap
I. Problem Introduction
I haven't used c # to connect to the database program for a long time. However, I found some information on the Internet and wrote it out. However, when I debug the preliminary program, I found it on con. open () is always an error,The error message returned when the database instance name cannot be found or the remote connection fails.Error40.
The error is as follows:
II. Environment Introduction
1. My database is sqlserver2008 and the development environment is vs2013 and. net4.0.
2. My database is installed with a named instance. Therefore, when writing a database connection string, the server name should be [computer name \ data instance name]
The database connection character is:
When a connection error is found, I thought it was an error caused by the incorrect environment of my sql2008 and vs2013. Later I opened the previous project and attached the database, change the data connection string and the error is the same.
Iii. troubleshooting
After finding an error for more than an hour, I found that when I used to write SQL connection characters, I added one before.@Symbol, because my string contains characters"\"If it is not added, an error is prompted, So I add it every time. However, during the last system reinstallation, the Instance name was changed to the name starting with "t. Then[\ T]This is a table character, so it is not an error, it is a correct use of escape symbols, because there is no error, it will forget to add"@".
The solution is to add @ in front of the string @.
Note:
When writing SQL connection strings or writing file paths on the windows platform, remember to add @ characters in front of them to form a good habit. Let's go to the trap.