2010-11-08 82 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