双引号内嵌双引号

4
我正在将以下连接字符串写入web.config文件,但是它给我报错了。请问正确的写法是什么?
<add name="stargaze_stargazeConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="D:\Work At DS\19th Jan\myastrolove.com_new\App_Data\dbName.mdf";Integrated Security=True;User Instance=True"/>
3个回答

14

web.config是XML格式的,因此您需要将内部引号转义为&quot;

<add name="stargaze_stargazeConnectionString1" 
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;D:\Work At DS\19th Jan\myastrolove.com_new\App_Data\dbName.mdf&quot;;Integrated Security=True;User Instance=True"/>

谢谢@Oded,它对我很有效。还要感谢您的紧急回复。 - gofor.net

2
在字符串中,将"替换为\"以正确转义它。
这意味着你的字符串应该像这样:
"<add name=\"stargaze_stargazeConnectionString1\" connectionString=\"Data Source=.\SQLEXPRESS;AttachDbFilename=\"D:\Work At DS\19th Jan\myastrolove.com_new\App_Data\dbName.mdf\";Integrated Security=True;User Instance=True\"/>"

-1 - 这不是 C# 字符串 - 它在 web.config 中。这仍然会为 OP 产生错误。 - Oded
看来我误解了问题。我没有看到 web.config 的部分。这使得你的答案是正确的,所以我会点赞它。 - Øyvind Bråthen

-1

难道不应该是这样吗:

<add name="stargaze_stargazeConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=\"D:\Work At DS\19th Jan\myastrolove.com_new\App_Data\dbName.mdf\";Integrated Security=True;User Instance=True"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接