non-alphanumeric (ex semicolon) in connectionstring

504 views Asked by At

Ive searched all over the web for a simple solution to my problem, but I find it weird that no one has some up with a way to "get a correct connection string if the password contains non-alphanumeric characters".

My problem:
I have a user which has a password containing one or more of these characters:

` ~ ! @ # $ % ^ & * ( ) _ + - = { } | \ : " ; ' < > ? , . /

since the connectionstring format is "KEY=VALUE;KEY2=VALUE2" it turns out to be a problem if the password contains a semi-colon of course.

So I did a little research and found these "connectionstring-rules"

  • All blank characters, except those placed within a value or within quotation marks, are ignored

  • Blank characters will though affect connection pooling mechanism, pooled connections must have the exact same connection string

  • If a semicolon (;) is part of a value it must be delimited by quotation marks (")

  • Use a single-quote (') if the value begins with a double-quote (")

  • Conversely, use the double quote (") if the value begins with a single quote (')

  • No escape sequences are supported

  • The value type is not relevant

  • Names are case iNsEnSiTiVe

  • If a KEYWORD=VALUE pair occurs more than once in the connection string, the value associated with the last occurrence is used

  • However, if the provider keyword occurs multiple times in the string, the first occurrence is used.

  • If a keyword contains an equal sign (=), it must be preceded by an additional equal sign to indicate that it is part of the keyword.

  • 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 then read a bunch of thread where people are trying to implement the above mentioned things into their "Format connectionstring method", but it seemed like even more scenarios came to light when they began.

My question is then:

Is there someone out there who has made a "FormatConnectionstring" method to use in a connectionstring - or am I doing something completely wrong here and my problem really exists elsewhere?

1

There are 1 answers

2
Marc Gravell On

Use SqlConnectionStringBuilder; either:

  • set properties, read ConnectionString (to create)
  • set ConnectionString, read properties (to parse)