To find all defined connection strings from your app.config, use the ConfigurationManager
(from System.Configuration).
It has an enumeration: ConfigurationManager.ConnectionStrings
which contains all entries in your <connectionStrings>
.
You can loop over it with this code:
foreach(ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
{
string name = css.Name;
string connString = css.ConnectionString;
string provider = css.ProviderName;
}
The Name
is just the symbolic name you give your connection string - it can be anything, really.
The ConnectionString
is the connection string itself.
The ProviderName
is the name of the provider for the connection, e.g. System.Data.SqlClient
for SQL Server (and others for other database system). If you omit the providerName=
attribute from your connection string in config, it defaults to SQL Server (System.Data.SqlClient).
Marc
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…