【问题标题】:How to use the escape keyword in C# for app.config [duplicate]如何在 C# 中为 app.config 使用转义关键字 [重复]
【发布时间】:2019-03-21 14:04:30
【问题描述】:

我有 app.config 文件,其中包含我的 Windows 窗体应用程序的以下代码。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
    </startup>
  <connectionStrings>
    <add name = "myConnection"
         connectionString = "Data Source=(LocalDB)\v11.0; AttachDbFilename = "C:\Users\USER\Documents\visual studio 2012\Projects\AliceAoi\WindowsFormsApplication2\Database2.mdf";Integrated Security=True"
         providerName = "System.Data.SqlClient" />
  </connectionStrings>
</configuration>

connectionString 属性给出错误。使用 \\ 而不是 \ 没有帮助。它指出需要空白。

我该如何解决这个问题?

【问题讨论】:

  • 我应该用 /` 更改所有 \ 吗?
  • @RonBeyer 是的,错误消息消失了。我会在运行应用程序时检查它是否有效。谢谢。

标签: c# xml connection-string app-config


【解决方案1】:

内部引号需要用&amp;quot;转义:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
  </startup>
  <connectionStrings>
    <add name = "myConnection"
         connectionString = "Data Source=(LocalDB)\v11.0;&quot;AttachDbFilename=&quot;C:\Users\USER\Documents\visual studio 2012\Projects\AliceAoi\WindowsFormsApplication2\Database2.mdf&quot;; IntegratedSecurity=True"
         providerName = "System.Data.SqlClient" />
  </connectionStrings>
</configuration>

问题在于 XML 解析器认为您的连接字符串在 \v11.0;" 之后结束,但它继续存在并且您收到其他错误。您必须使用 &amp;quot; 转义序列删除内部引号。

【讨论】:

  • 等等,你为什么在AttachDbFilename前后加"?这不是有效的 SQL 连接字符串。事实上,这些&amp;qout; 一个都不需要
  • @CamiloTerevinto 除非那里缺少引号,但这样做我收到了很多关于“不允许使用 AttachDbFilename 属性”的错误。
猜你喜欢
  • 2018-06-19
  • 1970-01-01
  • 2014-01-19
  • 2010-12-18
  • 2011-06-16
  • 2011-10-10
  • 2021-07-11
  • 1970-01-01
相关资源
最近更新 更多