Is there any connection string parser in C#?

Yes, there’s the System.Data.Common.DbConnectionStringBuilder class.

The DbConnectionStringBuilder class
provides the base class from which the
strongly typed connection string
builders (SqlConnectionStringBuilder,
OleDbConnectionStringBuilder, and so
on) derive. The connection string
builders let developers
programmatically create syntactically
correct connection strings, and parse
and rebuild existing connection
strings.

The subclasses of interest are:

System.Data.EntityClient.EntityConnectionStringBuilder
System.Data.Odbc.OdbcConnectionStringBuilder
System.Data.OleDb.OleDbConnectionStringBuilder
System.Data.OracleClient.OracleConnectionStringBuilder
System.Data.SqlClient.SqlConnectionStringBuilder

For example, to “peek out the Data Source” from a SQL-server connection string, you can do:

var builder = new SqlConnectionStringBuilder(connectionString);
var dataSource = builder.DataSource;

Leave a Comment