3

Is there a standard library or code snippet to get a value with a connection string like this?

string connstr = "DataServiceUrl=http://localhost/foo;" + 
        "RemoteServerConnection={server=http://localhost/foo2;interface=0.0.0.0;};" + 
        "publisherport=1234;StatisticsURL=http://localhost/foo3";

The whole inner connection property is kind of throwing this in a loop. I'd like to get specific values based on a key.

Here was the answer posted by John I used:

System.Data.Odbc.OdbcConnectionStringBuilder builder = new System.Data.Odbc.OdbcConnectionStringBuilder(); 
builder.ConnectionString = this.ConnectionString;
MessageBox.Show(builder["RemoteServerConnection"]);
5
  • 11
    See Connection String Builders Commented Dec 15, 2011 at 18:13
  • 2
    @JohnSaunders you really should just post that as a answer. Commented Dec 15, 2011 at 18:18
  • An answer that short would bring downvotes. I don't have time for a longer answer. Commented Dec 15, 2011 at 18:20
  • 1
    Here's the code to post and I'll give you credit for the answer System.Data.Odbc.OdbcConnectionStringBuilder builder = new System.Data.Odbc.OdbcConnectionStringBuilder(); builder.ConnectionString = this.ConnectionString; MessageBox.Show(builder["RemoteServerConnection"]); Commented Dec 15, 2011 at 18:25
  • 1
    @dave2118 you can post that that as an answer ... Commented Dec 15, 2011 at 18:48

1 Answer 1

5

Replacing "{" and "}" by (") does the trick:

string conn = "DataServiceUrl=http://localhost/foo;" +
    "RemoteServerConnection={server=http://localhost/foo2;interface=0.0.0.0;};" +
    "publisherport=1234;StatisticsURL=http://localhost/foo3";

var builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = conn.Replace("{", "\"").Replace("}", "\"");
var keys = builder.Keys;
var values = builder.Values;
string remoteServerConnection = (string)builder["RemoteServerConnection"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.