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?
I didn't really like all the answers here. So here is what I found.
You can use DbConnectionStringBuilder
directly:
var builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = settings.ConnectionString;
var server = builder["server"];
You can use DbConnectionStringBuilder, and you don't need any specific provider:
The following code:
var cnstr = "Data Source=data source value;Server=ServerValue";
var builder = new DbConnectionStringBuilder();
builder.ConnectionString = cnstr;
Console.WriteLine("Data Source: {0}", builder["Data Source"]);
Console.WriteLine("Server: {0}", builder["Server"]);
Outputs to console:
Data Source: data source value
Server: ServerValue
EDIT:
Since DbConnectionStringBuilder implements IDictionary you can enumerate the connection string parameters:
foreach (KeyValuePair<string, object> kv in builder)
{
Console.WriteLine("{0}: {1}", kv.Key, kv.Value);
}
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;
You want to use DbProviderFactory.CreateConnectionStringBuilder () which provides you a connection string builder/parser specific to your connector, but does not require you to use any connector specific classes.
Here's a couple lines of code that would parse any connection string into a dictionary:
Dictionary<string, string> connStringParts = connString.Split(';')
.Select(t => t.Split(new char[] { '=' }, 2))
.ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
And then you can access any part:
string dataSource = connStringParts["Data Source"];
Yes , You can do this using ConnectionStringBuilder Classes. Here is the list of available DbConnectionStringBuilder implementations for standard data providers:
System.Data.Odbc.OdbcConnectionStringBuilder
System.Data.OleDb.OleDbConnectionStringBuilder
System.Data.OracleClient.OracleConnectionStringBuilder
System.Data.SqlClient.SqlConnectionStringBuilder
here are sample example of parse connection string and display it's elements.
string conString = @"Data Source=.\sqlexpress;" +
"Database=Northwind;Integrated Security=SSPI;" +
"Min Pool Size=5;Max Pool Size=15;Connection Reset=True;" +
"Connection Lifetime=600;";
// Parse the SQL Server connection string and display it's properties
SqlConnectionStringBuilder objSB1 = new SqlConnectionStringBuilder(conString);
Response.Write("<b>Parsed SQL Connection String Parameters:</b>");
Response.Write(" <br/> Database Source = " + objSB1.DataSource);
Response.Write(" <br/> Database = " + objSB1.InitialCatalog);
Response.Write(" <br/> Use Integrated Security = " + objSB1.IntegratedSecurity);
Response.Write(" <br/> Min Pool Size = " + objSB1.MinPoolSize);
Response.Write(" <br/> Max Pool Size = " + objSB1.MaxPoolSize);
Response.Write(" <br/> Lifetime = " + objSB1.LoadBalanceTimeout);