我需要有关如何转换当前连接以及在我的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
中的配置:
答案 0 :(得分:1)
请更新您当前的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>
然后修改您的&#39; mdlConnection.vb&#39;代码如下:
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
请不要忘记添加名称空间&#39; Imports System.Configuration&#39;在你的&#mdlConnection.vb&#39;和参考&#39; System.Configuration&#39;在您的解决方案中。
答案 1 :(得分:0)
在解决方案资源管理器中双击项目的“我的项目”条目。从打开的窗口转到设置选项卡。将类型更改为Connection String,如下面的屏幕截图所示
或者,您可以将以下内容添加到<configuration>
开头标记下方的app.config文件中;
<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.
答案 2 :(得分:0)
以下是我的设置
这是我更新的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