遍历 VB.Net 中 App.Config 文件中的连接字符串

发布于 2024-09-30 21:53:34 字数 215 浏览 3 评论 0原文

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

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

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

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

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 the collection/listof connection strings.

The application is a WinForms VB.net .net 4.0 app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-10-07 21:53:34

这应该有效:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文