I have a connection string and I want to be able to peek out for example "Data Source". Is there a parser, or do I have to search the string?
See Question&Answers more detail:osI have a connection string and I want to be able to peek out for example "Data Source". Is there a parser, or do I have to search the string?
See Question&Answers more detail:osYes, 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;