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

时间:2016-08-21 11:55:43

标签: mysql vb.net oledb

我有一个非常古老的项目,它使用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

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解这个问题,但我会抓住它。

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

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

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

到这个

Dim comm As New MySqlCommand(sql, conn)

你应该能够保留现有的逻辑