Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config and quotes in connectionStrings

I have the following connection string, and you will notice "Provider's.Tests", notice the single quote, how do I enter this in to the web.config to make it valid?

<connectionStrings>
    <clear/>
    <add name="Provider" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Projects\Provider's.Tests\app_data\db.mdf";Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
like image 345
Coppermill Avatar asked Sep 11 '09 07:09

Coppermill


People also ask

Where are connectionStrings in web config?

Connection strings go inside a <connectionStrings> element. The traditional place to put <connectionStrings> seems to be immediately before <appSettings> but its precise location shouldn't matter.

How do you escape special characters in connection string?

Special characters can be escaped by encapsulating them in curly brackets {...}. When there is an un-escaped special character in the ODBC connection string the software will raise an error similar to: "Format of the initialization string does not conform to specification starting at index 90."

Is it safe to store connection string in web config?

The connection strings are mostly stored in web. config. It means that connection specific information such as database name, username, and password are stored as a clear text in a file. This is definitely a security concern for your Production servers.


3 Answers

I don't think its the Provider's that is the problem, It is the double quotes around the path.
Try to just remove it so it says AttachDbFilename=C:\Projects\Provider's.Tests\app_data\db.mdf;

If it is important in the connection string to have it, try encoding it: AttachDbFilename=&quot;C:\Projects\Provider's.Tests\app_data\db.mdf;&quot;

like image 130
awe Avatar answered Sep 27 '22 19:09

awe


You should encode both the quotation marks and apostropes. Quotation marks (") are encoded using &quot; and apostrophes (') are encoded using &apos;. The main issue here is the quotation marks, it might still work without encoding the apostrophes as you use quotation marks around the values.

<connectionStrings>
    <clear/>
    <add name="Provider" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\Projects\Provider&apos;s.Tests\app_data\db.mdf&quot;;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
like image 28
Guffa Avatar answered Sep 27 '22 17:09

Guffa


The single quote is not a problem in your case. It's the double quotes you have around the filename. You can escape it like this:

<add 
    name="Provider" 
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\Projects\Provider's.Tests\app_data\db.mdf&quot;;Integrated Security=True;User Instance=True" 
    providerName="System.Data.SqlClient"/>
like image 24
Darin Dimitrov Avatar answered Sep 27 '22 18:09

Darin Dimitrov