问题
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>
回答1:
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="C:\Projects\Provider's.Tests\app_data\db.mdf;"
回答2:
You should encode both the quotation marks and apostropes. Quotation marks (") are encoded using "
and apostrophes (') are encoded using '
. 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="C:\Projects\Provider's.Tests\app_data\db.mdf";Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
回答3:
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="C:\Projects\Provider's.Tests\app_data\db.mdf";Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
回答4:
you should use ' for the apostrophe and " ; for quotes for using special characters like this in the web.config file.
However, as other's have suggested you just need to remove the quotations as they are not required.
来源:https://stackoverflow.com/questions/1409548/web-config-and-quotes-in-connectionstrings