How to include special characters in connection string
By FoxLearn 6/21/2024 1:50:54 AM 1.02K
You will get an error if your sql server connection string contains an ampersand character (e.g.: p&ssword) are not encoded in the web.config file.
How to include special characters in connection string
Including special characters in a connection string typically depends on the programming language or framework you are using.
I think you've probably used an automatically generated password or a password that contains left angle brackets. In this situation you will encounter an unsuccessful connection to the database.
Here's a general approach:
Remember that web.config is an XML file so you must be encoded special characters.
Instead of using
<add name="db" connectionString="Data Source=.;Initial Catalog=mydb;User ID=sa;Password=p&ssw0rd;" providerName="System.Data.SqlClient" />
try
<!-- web.config escape characters --> <!-- c# connection string password with special characters --> <add name="db" connectionString="Data Source=.;Initial Catalog=mydb;User ID=sa;Password=p&ssw0rd;" providerName="System.Data.SqlClient" />
You can easily configure the web config password with special characters as shown above.
You will need to do this for all the 'special' characters below (to solve the connection string password with special characters)
< = < > = > " = " ' = ' & = &
This post answers the following common related questions:
sql server connection string password special characters
"&" character breaks passwords that are stored in the web.config file
'&' in password illegal character in web.config file
How to include ampersand in connection string using web.config file
How to escape quote in web.config connection string
How to escape an '&' in the password of a connection string in web.config file