Handle backslash in the connection string
12,506
Solution 1
The debugger may display it as ASDFG\\SQLEXPRESS
, but it's just escaping that backslash for display purposes.
Solution 2
use this in codebehind
string conString =
System.Configuration.ConfigurationManager
.ConnectionStrings["myDBConnectionString"].ToString();
Solution 3
always evaluated as "ASDFG\SQLEXPRESS"
Which is quite correct. Any actual problems opening the Db?
Author by
socialMatrix
Updated on November 20, 2022Comments
-
socialMatrix over 1 year
I have a following connection string in app.config
<add name="myDBConnectionString" connectionString="Data Source=ASDFG\SQLEXPRESS; Initial Catalog=ZAQ; Integrated Security=True;"/>
in my C# code then, when I get this string DB is always evaluated as
"ASDFG\\SQLEXPRESS"
I couldn't put @ since app.config doesn't like it. Also, if I say
ASDFG\\SQLEXPRESS
it gets evaluated as
ASDFG\\\\SQLEXPRESS
and not open the connection.
Thank You,
-
mellamokb about 13 yearsI've never had this problem. How are you retrieving and using the connection string? There is no concept of escaping with
"\"
inapp.config
configuration settings. -
manojlds about 13 yearsIsn't
ASDFG\SQLEXPRESS
what you want? Can't understand thein my C# code then, when I get this string DB is always evaluated as "ASDFG\SQLEXPRESS"
line -
WEFX about 13 years"ASDFG\SQLEXPRESS" looks correct. Do you have a type-o in your question?
-
mellamokb about 13 years@manojlds, @WEFX: I fixed OP's post. Apparently SO editor turns
"\\"
into"\"
when written as text in double-quotes. lol, how ironic. -
socialMatrix about 13 yearsvar connectionString= ConfigurationManager.ConnectionStrings["myDBConnectionString"].ConnectionString;
-
mellamokb about 13 years@socialMatrix: So what is the error message you are getting when trying to connect?
-
Lasse V. Karlsen about 13 yearsIs there an actual problem here? Like, can you connect to the database or not? If not, do you get an exception?
-
socialMatrix about 13 yearsHi everyone, this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.
-
-
socialMatrix about 13 yearsHi, this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.
-
socialMatrix about 13 yearsHi, this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.
-
socialMatrix about 13 yearsthis is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.