0

I gave the above string,I want to get the serveripaddress,DB1,uid and **** these values from that string.

1
  • At least show us what you already got. This question otherwise looks like "Hey, SO, solve my task for me" Commented Oct 27, 2009 at 9:34

4 Answers 4

6

Don't use a regex.

Parse the connection string using SqlConnectionStringBuilder and then access the keys from that.

var b = new SqlConnectionStringBuilder(myConnectionString);
var dataSource = b["Data Source"]; 
// etc.

Much easier, more maintainable and more reliable.

Sign up to request clarification or add additional context in comments.

Comments

2
data source=([^;]*);Initial Catalog=([^;]*);user id=([^;]*);Password=([^;"]*)

You then have the matches in groups 1 to 4. If you get bored you can name the groups; helps readability of the code, though rarely helps readability of the regex itself.

1 Comment

+1 since subramani only says thank you, and never heard of accepting or voting up apparently
1

There is no need to parse a connection string when the BCL can do it for you:

var builder = new OleDbConnectionStringBuilder(connectionString);
var provider = builder.Provider;
var dataSource = builder.DataSource;
var initialCatalog = builder["Initial Catalog"];
var userID = builder["User ID"];
var password = builder["Password"];

Note that for an OLE DB connection string, only the standard cross-provider properties have strongly-typed property names, all the rest are accessed via the indexer.

Comments

1

Isn't it easier to split value using ';' char and then split every item by '=' char?

This way your connection string could be edited by users and even item order could be changed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.