在VB.Net中迭代App.Config文件中的连接字符串

4

我想通过VB.net遍历App.Config中的所有连接字符串。

我希望能够完成以下两个步骤: 1. 获取所有连接字符串的数量 2. 将它们全部放入一个列表框中。

我已经尝试使用System.Configuration.ConfigurationSettings,但不确定如何准确地获取连接字符串的集合/列表。

该应用程序是一个WinForms VB.net .net 4.0应用程序。

1个回答

6

这应该可以工作:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' You need to add a reference to System.Configuration

    ' Uncomment to get the count:
    ' Dim count As Int32
    ' count = Configuration.ConfigurationManager.ConnectionStrings.Count

    Dim current As Configuration.ConnectionStringSettings

    For Each current In Configuration.ConfigurationManager.ConnectionStrings
        ListBox1.Items.Add(current.Name)
    Next
End Sub

注意:如果您的 app.config 中没有声明语句,那么您可能也会得到 LocalSqlServer,因为它是默认在 Machine.config 中定义的。

我明白了,你需要添加对System.Configuration的引用...谢谢。 - Jeremy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接