I am trying to iterate through all the connection strings in App.Config using VB.net.
I would like to: 1. Get a count of all the connection strings 2. Put them all into a listbox.
I have tried using System.Configuration.ConfigurationSettings but am unsure exactly how to get 开发者_StackOverflowthe collection/listof connection strings.
The application is a WinForms VB.net .net 4.0 app.
This should work:
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
Note: If you haven't got a statement in your app.config, you'll probably also get LocalSqlServer, as that's by default defined in Machine.config.
精彩评论