我可以从配置文件中读取连接字符串,但是我需要获取与连接字符串关联的元素名称。

例子

<connectionStrings>
<add
  name="LocalSqlServer"
  connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
  providerName="System.Data.SqlClient"   />
 </connectionStrings>

我需要从connectionString中获取LocalSqlServer。

最佳答案

根据documentation,它应该像这样工作:

ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;

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

关于c# - 如何从配置文件中获取ConnectionString名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4707138/

10-10 06:00