web.config and quotes in connectionStrings

瘦欲@ 提交于 2021-02-05 05:00:45

问题


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=&quot;C:\Projects\Provider's.Tests\app_data\db.mdf;&quot;




回答2:


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>



回答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=&quot;C:\Projects\Provider's.Tests\app_data\db.mdf&quot;;Integrated Security=True;User Instance=True" 
    providerName="System.Data.SqlClient"/>



回答4:


you should use ' for the apostrophe and &quot ; 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!