Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape space in connection string

I am trying to connect to a SQL server database using ADO. The password contains a space as the last character. When building the connection string, how should I escape the password?

MSDN says put it in single or double quotes but that does not seem to be working...

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx

This is what my connection string looks like:

Provider=SQLOLEDB;Persist Security Info=False;Initial Catalog=master;Data Source=test;uid=john;pwd="123 "

Thanks

like image 743
Z.D. Avatar asked Mar 20 '13 15:03

Z.D.


People also ask

How do you escape special characters in connection string?

Special characters can be escaped by encapsulating them in curly brackets {...}. When there is an un-escaped special character in the ODBC connection string the software will raise an error similar to: "Format of the initialization string does not conform to specification starting at index 90."

Can connection string have spaces?

If a value has preceding or trailing spaces it must be enclosed in single- or double quotes, ie Keyword=" value ", else the spaces are removed.


1 Answers

I can tell you what does work:

Provider=SQLOLEDB;Password="123 ";Persist Security Info=True;User ID=john;Initial Catalog=mydbname;Data Source=127.0.0.1
  1. When pwd keyword is used instead of Password it fails to connect - You need to use Password keyword which should be enclosed with double-quotes.
  2. uid instead of User ID works just fine - Id'e still use User ID because the connection builder creates it by default.
  3. Persist Security Info keyword (was mentioned in one of the comments) has no impact on connection - As @TLama well commented "You can connect, but you don't get the password back from the IDBProperties interface, if you keep it False."
  4. The order of keywords in the connection-string is not important. the IDBProperties interface parses this string into an internal "Properties" collection

Tested with SQL Server 2008 R2.

like image 124
kobik Avatar answered Oct 05 '22 22:10

kobik