开发者

How can I get ConnectionString Name from config file

I can read the connection strings from the config file, however I need to get the element name that is associated with connection string.

Example

<connectionStrings>
<add 
  name="LocalSqlServer" 
  connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=tr开发者_运维百科ue" 
  providerName="System.Data.SqlClient"   />
 </connectionStrings>

I need to get LocalSqlServer out of the connectionString.


According to the documentation it should work like this:

ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;

if (connections.Count != 0)
{
    foreach (ConnectionStringSettings connection in connections)
    {
        string name = connection.Name;
    }
}


Use

ConfigurationManager.ConnectionStrings[0].Name


You access it programmatically through the API.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜