连接字符串中的撇号(单引号) [英] Apostrophe (single quote) in connection string
问题描述
我在连接字符串中的’
字符时遇到麻烦。实体框架引发异常:
I'm having a trouble with the '
character in a connection string. Entity Framework throws an exception saying:
初始化字符串的格式不符合从索引开始的规范
Format of the initialization string does not conform to specification starting at index
我尝试了建议的答案此处和
I tried the answers suggested here and here to no avail.
我也尝试使用 SqlConnectionStringBuilder
类构造它,并得到相同的异常结果。
I also tried constructing it with SqlConnectionStringBuilder
class and got the same exception as a result.
有人可以帮我吗?
预先感谢。
编辑:
连接字符串确实位于web.config文件中,它看起来像这样:
The connectionstring is indeed in the web.config file and it looks like this:
<add name="TestEntities" connectionString="metadata=res://*/DAL.TestModel.csdl|res://*/DAL.TestModel.ssdl|res://*/DAL.TestModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\testsource;initial catalog=testdb;User Id=testuser;Password=test'password;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
问题在于 Password = test'password
位。
- 我不能将其用双引号引起来,因为它已经被它们包围了。
- 我不能把它放在
&
-s之间,因为它也已经被它们包围了。 - 我不能将其用单引号引起来,因为单引号本身就是我要逃脱的内容。
- I can't put it in double quotes because it's already surrounded by them.
- I can't put it between
"
-s because it's already surrounded by them too. - And I can't put it in single quotes because the single quote itself is what I'm trying to escape.
Password ='test&'password'
不起作用。
推荐答案
我终于迷失了一个正确答案。看起来,您必须用单引号将值引起来,并另外复制内撇号以使其逃逸,如下所示:
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.
感谢大家的参与。
这篇关于连接字符串中的撇号(单引号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!