Apostrophe (single quote) in connection string

。_饼干妹妹 提交于 2019-12-05 17:06:01

I have finally stumbled upon a correct answer. It appears, you have to surround the value with single quotes and additionally duplicate the inner apostrophe in order to escape it, like this:

Password='test''password'

This is such a dumb problem to waste this much time on. I hope it saves somebody time in the future.

Thanks to everyone for participating.

Assuming it is the password which contains a single quote, you should try enclosing the password in xml encoded double-quotes, something like this :

<connectionStrings>
  <add name="MyConnectionString" connectionString="Data Source=.;
    Initial Catalog=MYDB; User ID=MyUser;Password=&quot;my'password&quot;;
    providerName="System.Data.SqlClient";/>
</connectionStrings>

Assuming you are not in Xml context (as mentionned by @PanagiotisKanavos ) the ConnectionString initialization would look like :

var conn = new SqlConnection("Data Source=.;
    Initial Catalog=MYDB; User ID=MyUser;Password=\"my'password\";
    providerName="System.Data.SqlClient")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!