Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I wish to connect my sqlexpress2016 database by code.
For that I try to build a connection string, but it's giving error ..

Any guidance's will be helpful.


static String MyDBConStr = "Data Source=.\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";


Error :
.\sqlexpress           class system.string Represnts text as a series of Unicode characters       Unrecognized escape sequence


Thanks Regards
PARAMAN

What I have tried:

Build Database connection string
Posted
Updated 28-May-17 22:28pm

Do you have an escape character in the middle of the string (\)... 2.4.4.5 String literals (C#)[^]
You have two options:
1. Double the escape char .\\sqlexpress
2. Use @ to disable escape character for this string only @"..."
 
Share this answer
 
Comments
Paramu1973 29-May-17 4:34am    
Hi, Its a local sqlexpress windows authentication created sql database...
& receiving the following output.

thanks

n exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: 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: SQL Network Interfaces,
error: 25 - Connection string is not valid)
'\' is a special character in C# - it indicates that the following character is a "special code" and should be combined to produce a new character. For example, "\n" is a newline, "\"" is a double quote, and "\\" is a backslash.
Try this:
static String MyDBConStr = "Data Source=.\\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";
Or better, prefix teh strign with '@' to turn off backslashes:
static String MyDBConStr = @"Data Source=.\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";

But you shouldn't do that anyway: connection strings should be stored in a configuration file, so you don't have to recompile your application for each new release or change in the DB server!
 
Share this answer
 
Comments
Paramu1973 29-May-17 5:44am    
Thanks for the replies
OriginalGriff 29-May-17 5:49am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900