Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 password ending in a semicolon

Let's say I have a password that looks like this: password;

How can I get it to work with a semicolon as the last character. The password works in SSMS and ODBC, but not with the connection string. I've tried " around it but that does not seem to work.

<add name="DbConn" connectionString="Data Source=LOCALHOST;Database=MYDB;Trusted_Connection=no;User Id=myuser;Password=password;" providerName="System.Data.SqlClient" />

This is for an ASP.NET web application. As far as I can tell, it is impossible. UPDATE: It IS possible!

like image 219
smoore4 Avatar asked Apr 11 '14 13:04

smoore4


People also ask

How do I create a username and password for SQL Server 2008 r2?

Right-click on the Logins folder and select ​New Login. If you want to assign rights to a Windows account, select Windows authentication. If you want to create an account that exists only in the database, select SQL Server authentication. Provide the login name in the text box.

What is the password characters length in SQL Server?

Passwords can be up to 128 characters long. Use passwords that are as long and complex as possible.


3 Answers

Encapsulate your password in single quotes. e.g. given the password iloveachallenge; your connection string should contain Password='iloveachallenge;';.

I am using the following code to connect to SQL Server 2008 R2.

  var sqlConnection = new SqlConnection()
            {
                ConnectionString = "Server=DT2719MOD;Database=abs2;User Id=TestUserLogon;Password='iloveachallenge;';"

            };
        sqlConnection.Open();
        Console.WriteLine(sqlConnection.State);
        sqlConnection.Close();

Edit: Also tried to use the connection string that you have and it works on my machine.

ConnectionString="Data Source=DT2719MOD;Database=abs2;Trusted_Connection=no;User Id=TestUserLogon;Password='iloveachallenge;';" 
like image 131
abhi Avatar answered Oct 17 '22 19:10

abhi


To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotes.

From: http://msdn.microsoft.com/en-us/library/windows/desktop/ms722656(v=vs.85).aspx

Basically you have there all the escaping procedures of a connection string.

like image 34
Dumitrescu Bogdan Avatar answered Oct 17 '22 19:10

Dumitrescu Bogdan


Yes, this is possible. The answer is given in Pete's answer in this thread: Escape ;(semicolon) in odbc connection string in app.config file

Basically, you need to put single quotes around the Username/Password fields, not the characters you want to escape, which is where I was going wrong.

To steal Pete's example:

initial catalog=myDB;UserId=MyUser;Password=abc;123;multipleactiveresultsets=True;

needs to become:

initial catalog=myDB;UserId='MyUser';Password='abc;123';multipleactiveresultsets=True;
like image 34
WiredEarp Avatar answered Oct 17 '22 17:10

WiredEarp