【问题标题】:Iterate through connections strings in App.Config file in VB.Net遍历 VB.Net 中 App.Config 文件中的连接字符串
【发布时间】:2011-05-06 10:40:27
【问题描述】:

我正在尝试使用 VB.net 遍历 App.Config 中的所有连接字符串。

我想: 1.获取所有连接字符串的计数 2. 将它们全部放入一个列表框中。

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

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

【问题讨论】:

    标签: vb.net winforms app-config connection-string


    【解决方案1】:

    这应该可行:

    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 的引用...谢谢。
    猜你喜欢
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2013-02-01
    • 2015-09-16
    相关资源
    最近更新 更多