There's also the SqlConnectionStringBuilder
class:
var connectionString =
new System.Data.SqlClient.SqlConnectionStringBuilder("Data Source=ServerName;Initial Catalog=DatabaseName;Persist Security Info=True;Connection Timeout =60;Integrated Security=SSPI");
Console.WriteLine(connectionString.DataSource);
Console.WriteLine(connectionString.InitialCatalog);
// ...
Response to comment:
To get the connection strings directly from configuration, use:
foreach (ConnectionStringSettings c in System.Web.Configuration.WebConfigurationManager.ConnectionStrings)
{
var connectionString = new SqlConnectionStringBuilder(c.ConnectionString)
//connectionString.DataSource; // server name
//connectionString.InitialCatalog; // database name
}
Note that this will include connection strings in your machine.config (e.g. data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
). If you don't want to see that you can filter it in code or add a <clear />
element to your web.config before your connection strings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…