c# - How to include ampersand in connection string? -
i'm using entity framework 4 simple app , bake connection credentials 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, password (which cannot change) contains ampersand. asp.net throws: configuration error: error occurred while parsing entityname. line xx, position yyy.
if replace ampersand in password &
, sqlexception: login failed user 'myuser'.
trick works, i'm guessing failing because technically connection string inside connection string.
what should here? of classes include code like:
using (var context = new myentities()) { // work }
update: turns out credentials using domain account, need integrated security=true
in connection string rather password.
encoding ampersand indicated in accepted answer should work fine, though haven't tested it.
you'll need use escape sequences xml document, .config files are.
- ampersand = & =
&
- greater = > =
>
- less = < =
<
- apostrophe = ' =
'
- quote = " =
"
you can use cdata
tag can use these illegal characters
<![cdata[
, ends ]]>
<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>
Comments
Post a Comment