2010-11-08 86 views
4

我想通过VB.net在App.Config中遍历所有连接字符串。在VB.Net App.Config文件中迭代通过连接字符串

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

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

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

回答

5

这应该工作:

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中定义的默认。

+0

我看到了,你必须添加对System.Configuration ... Thankyou的引用。 – Jeremy 2010-11-08 04:11:42