Snippet Fire

Beta
 Log In    |   Sign Up

.NET : Parse A SQL Server Connection String

Added on Jan-19-2012 by mikeSarg
For .NET

Tags : c#connection stringregex

This regex snippet returns the database instance name, host name, and database name from a connection string.

DOWNLOAD

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


    

Report Snippet

blog comments powered by Disqus