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=&quot;data source=localhost\dev;initial catalog=mydb;userid=myuser;password=jack&jill;multipleactiveresultsets=true&quot;"           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 &amp;, 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 = & = &amp;
  • greater = > = &gt;
  • less = < = &lt;
  • apostrophe = ' = &apos;
  • quote = " = &quot;

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=&quot;         data source=localhost\dev;         initial catalog=mydb;userid=myuser;         password=<![cdata[jack&jill]]>;         multipleactiveresultsets=true&quot;"          providername="system.data.entityclient" /> </connectionstrings> 

Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -