如何使用 app.config 从 vb.net 连接到 MySQL 数据库

How to connect to MySQL database from vb.net using app.config

我需要有关如何转换我当前的连接并在我的 vb.net 项目中使用 app.config 进行迁移的帮助,因此当我传输或编辑我的数据库凭据时,我不需要重新编译新的安装程序对于我的程序。在此先感谢队友!

这是我当前的连接:

(代码在我的 mdlConnection.vb 中)

Module mdlConnection
    Public connString1 As String = "server=localhost;user id=root;password=;Database=sampleprog"
    Public conn As MySqlConnection = New MySqlConnection(connString1)
    Public sql As String = ""

    Public Function ExecuteQuery(ByVal query As String) As DataTable
        Dim sqlDT As New DataTable
        Try
            Dim sqlCon As New MySqlConnection(connString1)
            Dim sqlDA As New MySqlDataAdapter(query, sqlCon)
            Dim sqlCB As New MySqlCommandBuilder(sqlDA)
            sqlDA.Fill(sqlDT)
        Catch ex As Exception
        End Try
        Return sqlDT
    End Function
End Module

这是我里面的配置 app.config:

在解决方案资源管理器中双击项目的 "My Project" 条目。从打开的 window 转到设置选项卡。将类型更改为连接字符串,如下面的屏幕截图所示

或者,您可以将以下内容添加到 app.config 文件的 <configuration> 开始标记下方;

<configuration>
    <connectionStrings>
        <add name="<Desired name here>" connectionString="server=localhost;user id=root;password=;Database=sampleprog" />
    </connectionStrings>
    <startup>
    ...
</configuration>

然后您可以在您的代码中访问连接字符串,如下所示。

Dim con = My.Settings.DbConnection ' provided you named your connection string DbConnection in the first step above.

这是我的设置

这是我更新的 mdlConnection

这是我更新的 mdlConnection

Dim con = My.Settings.DbConnection
Public conn As MySqlConnection = New MySqlConnection(con)
Public sql As String = ""

Public Function ExecuteQuery(ByVal query As String) As DataTable
    Dim sqlDT As New DataTable
    Try
        Dim sqlCon As New MySqlConnection(con)
        Dim sqlDA As New MySqlDataAdapter(query, sqlCon)
        Dim sqlCB As New MySqlCommandBuilder(sqlDA)
        sqlDA.Fill(sqlDT)
    Catch ex As Exception
    End Try
    Return sqlDT
End Function

请更新您当前的 app.config 文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
        </startup>

        <connectionStrings>
            <add name="MySqlConnectionString" connectionString="SERVER=localhost;DATABASE=sampleprog;UID=root;PASSWORD=;" />
        </connectionStrings>

    </configuration>

然后修改您的 'mdlConnection.vb' 代码如下:

    Module mdlConnection
        Public connString1 As String = ConfigurationManager.ConnectionStrings("MySqlConnectionString").ToString()
        Public conn As MySqlConnection = New MySqlConnection(connString1)
        Public sql As String = ""

        Public Function ExecuteQuery(ByVal query As String) As DataTable
            Dim sqlDT As New DataTable
            Try
                Dim sqlCon As New MySqlConnection(connString1)
                Dim sqlDA As New MySqlDataAdapter(query, sqlCon)
                Dim sqlCB As New MySqlCommandBuilder(sqlDA)
                sqlDA.Fill(sqlDT)
            Catch ex As Exception
            End Try
            Return sqlDT
        End Function
    End Module

请不要忘记在您的 'mdlConnection.vb' 中添加命名空间 'Imports System.Configuration' 并在您的解决方案中添加引用 'System.Configuration'。