Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to include ampersand in connection string?
    text
    copied!<p>I'm using Entity Framework 4 for a simple app and would like to bake my connection credentials into the following connection string:</p> <pre><code>&lt;connectionStrings&gt; &lt;add name="MyEntities" connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&amp;jill;MultipleActiveResultSets=True&amp;quot;" providerName="System.Data.EntityClient" /&gt; &lt;/connectionStrings&gt; </code></pre> <p>However, the password (which I cannot change) contains an ampersand. ASP.NET throws: <code>Configuration Error: An error occurred while parsing EntityName. Line XX, position YYY.</code></p> <p>If I replace the ampersand in the password with <code>&amp;amp;</code>, I get a <code>SqlException: Login failed for user 'myUser'.</code> Usually this trick works, but I'm guessing that something is failing because this is technically a connection string inside a connection string.</p> <p>What should I do here? Most of my classes include code like:</p> <pre><code>using (var context = new MyEntities()) { // do work } </code></pre> <hr> <p><strong>Update:</strong> It turns out that the credentials I am using are a domain account, so what I really need is <code>Integrated Security=True</code> in the connection string rather than a password.</p> <p>Encoding the ampersand as indicated in the accepted answer <em>should</em> work fine, though I haven't tested it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload