How to include ampersand in connection string?

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 = < = &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>

Leave a Comment