Parse connection string

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet parses the specified connection string to find the host name, instance name, and database name. It returns them in a GroupCollection .

using System.Text.RegularExpressions;
...
const string CONNECTION_STRING_PATTERN 
   = @"=(?<sqlServerName>.+);Initial Catalog=(?<databaseName>\w+);";
 
static readonly Regex ConnectionString_Regex
   = new Regex (CONNECTION_STRING_PATTERN, RegexOptions.IgnoreCase);
 
static GroupCollection ParseConnectionString (string inputString)
{
   Match match = ConnectionString_Regex.Match (inputString);
   return match.Groups;
}

Personal tools