如何在连接字符串中包含&符? [英] How to include ampersand in connection string?

查看:40
本文介绍了如何在连接字符串中包含&符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个简单的应用程序使用Entity Framework 4,并且希望将我的连接凭据烘焙到以下连接字符串中:

<connectionStrings>
    <add name="MyEntities"    
         connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

但是,密码(我不能更改)包含&"号. ASP.NET抛出: Configuration Error: An error occurred while parsing EntityName. Line XX, position YYY.

如果我用&amp;替换密码中的&符号,通常会得到SqlException: Login failed for user 'myUser'.这个技巧,但是我猜测有些故障,因为从技术上讲,这是连接字符串中的连接字符串.

我应该在这里做什么?我的大部分课程都包含如下代码:

using (var context = new MyEntities()) {
   // do work
}


更新:事实证明,我使用的凭据是域帐户,所以我真正需要的是连接字符串中的Integrated Security=True而不是密码.

按照接受的答案中的符号对&编码,应该可以正常工作,尽管我尚未对其进行测试.

解决方案

您将需要像对任何XML文档(所有.config文件都是这样)一样使用转义序列.

  • Ampersand =& = &amp;
  • 大于=> = &gt;
  • 小于=< = &lt;
  • 撇号='= &apos;
  • 报价="= &quot;

您还可以使用CDATA标记,以便可以使用这些非法字符

<![CDATA[并以]]>

结尾

<connectionStrings>
    <add name="MyEntities" connectionString="
        metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
        provider=System.Data.SqlClient;
        provider connection string=&quot;
        Data Source=localhost\DEV;
        Initial Catalog=MyDB;UserId=myUser;
        Password=<![CDATA[jack&jill]]>;
        MultipleActiveResultSets=True&quot;" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

I'm using Entity Framework 4 for a simple app and would like to bake my connection credentials into the following connection string:

<connectionStrings>
    <add name="MyEntities"    
         connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

However, the password (which I cannot change) contains an ampersand. ASP.NET throws: Configuration Error: An error occurred while parsing EntityName. Line XX, position YYY.

If I replace the ampersand in the password with &amp;, I get a SqlException: Login failed for user 'myUser'. Usually this trick works, but I'm guessing that something is failing because this is technically a connection string inside a connection string.

What should I do here? Most of my classes include code like:

using (var context = new MyEntities()) {
   // do work
}


Update: It turns out that the credentials I am using are a domain account, so what I really need is Integrated Security=True in the connection string rather than a password.

Encoding the ampersand as indicated in the accepted answer should work fine, though I haven't tested it.

解决方案

You'll need to use escape sequences like you would for any XML document, which is all the .config files are.

  • Ampersand = & = &amp;
  • Greater Than = > = &gt;
  • Less Than = < = &lt;
  • Apostrophe = ' = &apos;
  • Quote = " = &quot;

You can also use the CDATA tag so that you can use these illegal characters

<![CDATA[ and ends with ]]>

<connectionStrings>
    <add name="MyEntities" connectionString="
        metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
        provider=System.Data.SqlClient;
        provider connection string=&quot;
        Data Source=localhost\DEV;
        Initial Catalog=MyDB;UserId=myUser;
        Password=<![CDATA[jack&jill]]>;
        MultipleActiveResultSets=True&quot;" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

这篇关于如何在连接字符串中包含&符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆