MYSQL Access 中旧 VB.NET 项目中的连接字符串

MYSQL connection string in old VB.NET project in Access

我有一个非常古老的项目,它使用 Access DB (.mdb) 并使用来自不同页面的各种连接。一些包括 OLE DB、DAO、ADO。我有超过 200 页的各种联系。我要转到 MySQL 并想清理这个烂摊子。从 OLEDB 开始,我遇到了一个连接问题,该连接允许我保留我的其余代码(或者即使它可以完成?)

是的,我查看了以下的各种示例:http://www.connectionstrings.com/net-framework-data-provider-for-ole-db/

这是我需要移动到 MySQL 连接的众多页面之一:

Partial Class mysql_a_Checkoff
Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button1.DataBinding
        '*** Code to insert class checkoff into class_record table ***
        For index As Integer = 0 To GridView1.Rows.Count - 1
            'Programmatically access the Checkbox from the TemplateField
            Dim cb As CheckBox = CType(GridView1.Rows(index).FindControl("RowLevelCheckBox"), CheckBox)
            'If it is checked, insert it into class records table
            If cb.Checked Then
                'Code to insert into DB table
                Dim FDID As String = GridView1.Rows(index).Cells(1).Text.ToString
                Dim Instructor As String = User.Identity.Name()
                Dim DateCompleted As Date = TextBox1.Text
                Dim Completed As Boolean = True
                Dim Enrolled As Boolean = False
                Dim UserName As String = GridView1.Rows(index).Cells(4).Text.ToString
                Dim ClassName As String = DropDownList1.SelectedValue.ToString
                Dim ClassDate As Date = CDate(TextBox1.Text)
                Dim WaitListed As Boolean = False
                Dim Walkin As Boolean = False
response.write("Yes - ")
                InsertClassRecord(UserName, Instructor, DateCompleted, Completed, Enrolled, ClassName, ClassDate, WaitListed, Walkin)
            End If
        Next

        Response.Redirect("i_toc.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            TextBox1.Text = Now.Date
        End If
    End Sub

    Public Function InsertClassRecord(ByVal UserName As String, ByVal Instructor As String, _
                                      ByVal DateCompleted As Date, ByVal Completed As Boolean, _
                                      ByVal Enrolled As Boolean, ByVal ClassName As String, _
                                      ByVal ClassDate As Date, ByVal WaitListed As Boolean, _
                                      ByVal Walkin As Boolean) As Object

        Dim connStr As String = "Provider=SQLOLEDB;Server=localhost;Database=mysql_training;Uid=myUsr;Pwd=myPwd;"
        conn.ConnectionString = connStr
        conn.Open()
        Dim sql As String = "INSERT INTO EnrollmentsTbl (" & _
        "[UserName],[SubmitTime],[ClassTime],[ClassDate],[Enrolled],[ClassName],[WaitListed]," & _
        "[Instructor],[DateCompleted],[Completed],[Walkin]) VALUES " & _
        "(@UserName, @SubmitTime, @ClassTime, @ClassDate, @Enrolled, @ClassName, @WaitListed, " & _
        "@Instructor, @DateCompleted, @Completed, @Walkin) "

        Dim comm As New Data.OleDb.OleDbCommand(sql, conn)
        comm.Parameters.AddWithValue("@UserName", UserName)
        comm.Parameters.AddWithValue("@SubmitTime", DateTime.Now.ToString())
        comm.Parameters.AddWithValue("@ClassTime", "0800")
        comm.Parameters.AddWithValue("@ClassDate", ClassDate)
        comm.Parameters.AddWithValue("@Enrolled", Enrolled)
        comm.Parameters.AddWithValue("@ClassName", ClassName)
        comm.Parameters.AddWithValue("@WaitListed", WaitListed)
        comm.Parameters.AddWithValue("@Instructor", Instructor)
        comm.Parameters.AddWithValue("@DateCompleted", DateCompleted)
        comm.Parameters.AddWithValue("@Completed", Completed)
        comm.Parameters.AddWithValue("@Walkin", Walkin)

        Dim result As Integer = comm.ExecuteNonQuery()
        conn.Close()
        Return True
    End Function

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        e.Row.Cells(4).Visible = False
    End Sub
End Class

不确定我是否完全理解这个问题,但我会尝试一下。

下载 MySQL NET 连接器并添加对项目的引用。在任何使用 OleDBConnection 或 OleDBCommand 的地方,您都需要将其分别更改为 MySqlConnection 和 MySqlCommand。这应该允许您尽可能多地重用现有逻辑。

例如,在您的 InsertClassRecord 方法中,您可以更改此

Dim comm As New Data.OleDb.OleDbCommand(sql, conn)

至此

Dim comm As New MySqlCommand(sql, conn)

而且你应该能够保持现有的逻辑