连接字符串中的撇号(单引号)

编程入门 行业动态 更新时间:2024-10-10 10:29:58
本文介绍了连接字符串中的撇号(单引号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在连接字符串中的’字符时遇到麻烦。实体框架引发异常:

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=&quot;data source=.\testsource;initial catalog=testdb;User Id=testuser;Password=test'password;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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 &quot;-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.

    感谢大家的参与。

    更多推荐

    连接字符串中的撇号(单引号)

    本文发布于:2023-10-12 16:13:21,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1485236.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   单引号

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!