12

I want to extract from a connectionString (a string variable) the server and database names. The name of the server and database change as we move from DEV to STAGE and then PROD.

Here's an example:

Data Source=SERVER_XYZ;Initial Catalog=DATABASE_XYZ;User ID=us;Password=pass

Data Source=SERVER_XYZPQR;Initial Catalog=DATABASE_XYZPQR;User ID=us;Password=pass

Notice the name changes (as well as lengths of the whole string).

How do I capture the data source and initial catalog without knowing the length it's going to be? So that on form load, it'll get the Server & Database name to display to the user (so he/she can see which server and database he/she is connected to?

4
  • What language are you working in? Commented Aug 3, 2012 at 21:29
  • 1
    @Daniel not when .net provides a class to handle this Commented Aug 3, 2012 at 21:58
  • 3
    @Daniel: you have a problem and decide to solve it using a regex. Now you have two ... Commented Aug 4, 2012 at 8:19
  • IMHO regex's are a great addition to a developers toolbox, just that I don't think it should be used as a hammer... Commented Aug 4, 2012 at 9:04

2 Answers 2

40

You can use the connection string builder class which once constructed has datasource and initial catalog properties

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

string connStr = "Data Source=SERVERx;Initial Catalog=DBx;User ID=u;Password=p"; 

var csb = new SqlConnectionStringBuilder(connStr);

string dataSource = csb.DataSource;
string initialCatalog = csb.InitialCatalog;

Let the .net framework do the work for you ;) no messing about with substrings or regexs

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

6 Comments

ah thanks! a different way of doing it.
+1 I like this better than the accepted answer because it's less prone to bugs IMAO.
@user1336632 you shouldn't think of this as an alternative solution; this is the correct solution! When the .net framework provides a class to handle your scenario, you really should be using it.
+1 : Regex is fun, but this makes far much more sense than my answer. I didn't even know the SqlConnectionStringBuilder class existed! Using as a boiler plate now for building my connection strings now.
Very clean and elegant solution! nice! I did the same with OracleConnectionStringBuilder - worked like a charm , saved me a lot of headache
|
6

A C# Regex solution:

String input = "Data Source=SERVER_XYZ;Initial Catalog=DATABASE_XYZ;User ID=us;Password=pass";

// Match the server:
Match serverMatch = Regex.Match(input, @"Source=([A-Za-z0-9_.]+)", RegexOptions.IgnoreCase);

// Match the database:
Match databaseMatch = Regex.Match(input, @"Catalog=([A-Za-z0-9_]+)", RegexOptions.IgnoreCase);

// Get the string
if (serverMatch.Success)
{
  String server = serverMatch.Groups[1].Value;
}

Keep in mind valid characters for URLs:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=

3 Comments

and then to display it simply do this? lblServer.Text = serverMatch.ToString(); lblDatabase.Text = databaseMatch.ToString();
that worked. what if I don't want to display the "Source=" and "Catalog=" ?
that worked. thanks! :) ps. regex and Linq have some real ugly syntaxes!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.