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
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."
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.
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
pwd
keyword is used instead of Password
it fails to connect - You need to use Password
keyword which should be enclosed with double-quotes.uid
instead of User ID
works just fine - Id'e still use User ID
because the connection builder creates it by default. 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."
IDBProperties
interface parses this string into an internal "Properties" collection
Tested with SQL Server 2008 R2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With