如何在连接字符串中包含&符? [英] How to include ampersand in connection string?
问题描述
我正在为一个简单的应用程序使用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="Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
但是,密码(我不能更改)包含&"号. ASP.NET抛出:
Configuration Error: An error occurred while parsing EntityName. Line XX, position YYY.
如果我用&
替换密码中的&符号,通常会得到SqlException: Login failed for user 'myUser'.
这个技巧,但是我猜测有些故障,因为从技术上讲,这是连接字符串中的连接字符串.>
我应该在这里做什么?我的大部分课程都包含如下代码:
using (var context = new MyEntities()) {
// do work
}
更新:事实证明,我使用的凭据是域帐户,所以我真正需要的是连接字符串中的Integrated Security=True
而不是密码.
按照接受的答案中的符号对&编码,应该可以正常工作,尽管我尚未对其进行测试.
您将需要像对任何XML文档(所有.config文件都是这样)一样使用转义序列.
- Ampersand =& =
&
- 大于=> =
>
- 小于=< =
<
- 撇号='=
'
- 报价="=
"
您还可以使用CDATA
标记,以便可以使用这些非法字符
<![CDATA[
并以]]>
<connectionStrings>
<add name="MyEntities" connectionString="
metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=localhost\DEV;
Initial Catalog=MyDB;UserId=myUser;
Password=<![CDATA[jack&jill]]>;
MultipleActiveResultSets=True""
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="Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True""
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 &
, 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 = & =
&
- Greater Than = > =
>
- Less Than = < =
<
- Apostrophe = ' =
'
- Quote = " =
"
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="
Data Source=localhost\DEV;
Initial Catalog=MyDB;UserId=myUser;
Password=<![CDATA[jack&jill]]>;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
这篇关于如何在连接字符串中包含&符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!