Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - Escape ;(semicolon) in odbc connection string in app.config file

I have created a windows form with certain fields. I am trying to interact with oracle database through ODBC DSN connections.

I have an issue in the below connection string in app.config .

In the connection string password contains semicolon(here abc;45). I am getting an error:

"Format of the initialization string does not conform to specification starting at index 35"

while i am trying to access this connection string using

OdbcConnection connection = new OdbcConnection(connection_string);

in C# code.

Below is my connection string.

<add name="ConnectionString_T1" connectionString="DSN=CLA_T5;Uid=abc;Pwd=abc;45" providerName="System.Data.Odbc" />

 OdbcConnection connection = new OdbcConnection(connection_string);

PS: I tried putting this password in double quotes/single quote/" . But no use. Still facing this error. I have tried putting all the escape sequences like ",double quote, single quote,/" etc..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rikitikitik is partly correct about escaping []{(),;?*=!@ characters in an ODBC Connection String by surrounding the value with {} , but misses a subtle but extremely important ODBC Connection String escape rule that is NOT documented by Microsoft about escaping }.

PASS CASE: connectionString="DSN=CLA_T5;Uid=abc;Pwd={abc;45}"

Works correctly to escape the ;, but will fail when the password is

FAIL CASE: connectionString="DSN=CLA_T5;Uid=abc;Pwd={abc;}45}"

Because the first } character is interpreted as the closing brace of the escape pair, rather than the second (correct) }.

To correct for this, you have to manually escape the } with a second }

PASS CASE: connectionString="DSN=CLA_T5;Uid=abc;Pwd={abc;}}45}", which will be read as abc;}45} by the ODBC engine.

This appears to be a totally undocumented, despite several MSDN sources outlining the enclosing {} escaping Rikitikitik mentions.

Documentation failing to mention the interior } escape method:

However, the interior } escape method is clearly observable with a quick .net test harness:

OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder
builder.Driver = "{SomeDriver}"
builder.Add("UID", "user");
builder.Add("PWD", "abc;}45"); 

MessageBox.Show(builder.ConnectionString) // observe PWD's escaped value of "{abc;}}45}"

try
{
    using (OdbcConnection conn = new OdbcConnection(builder.ConnectionString))
    {
        //           
        MessageBox.Show("SUCCEEDED");
    }
}
catch (Exception ex)
{
    MessageBox.Show($"{ResourceManager.GetString("FAILED: ")} {ex.Message}");
}

where SomeDriver has a user user with password abc;}45


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...