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?

Share:
12,506
socialMatrix
Author by

socialMatrix

Updated on November 20, 2022

Comments

  • socialMatrix
    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
      mellamokb about 13 years
      I've never had this problem. How are you retrieving and using the connection string? There is no concept of escaping with "\" in app.config configuration settings.
    • manojlds
      manojlds about 13 years
      Isn't ASDFG\SQLEXPRESS what you want? Can't understand the in my C# code then, when I get this string DB is always evaluated as "ASDFG\SQLEXPRESS" line
    • WEFX
      WEFX about 13 years
      "ASDFG\SQLEXPRESS" looks correct. Do you have a type-o in your question?
    • mellamokb
      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
      socialMatrix about 13 years
      var connectionString= ConfigurationManager.ConnectionStrings["myDBConnectionString‌​"].ConnectionString;
    • mellamokb
      mellamokb about 13 years
      @socialMatrix: So what is the error message you are getting when trying to connect?
    • Lasse V. Karlsen
      Lasse V. Karlsen about 13 years
      Is there an actual problem here? Like, can you connect to the database or not? If not, do you get an exception?
    • socialMatrix
      socialMatrix about 13 years
      Hi everyone, this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.
  • socialMatrix
    socialMatrix about 13 years
    Hi, this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.
  • socialMatrix
    socialMatrix about 13 years
    Hi, this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.
  • socialMatrix
    socialMatrix about 13 years
    this is crazy, but I was using SQLite command to connect to SQL server DB :-( ... thanks a lot for your help.