2016-08-21 53 views
0

我有一個非常舊的項目,它使用Access DB(.mdb)並使用來自各個頁面的各種連接。有些包括OLE DB,DAO,ADO。我有超過200頁的各種連接。我正在轉向MySQL並希望清理這個混亂。與我在同一個連接的麻煩,讓我保持我的代碼的其餘部分(?或者即使是可以做到的)OLEDB開始Access中的舊VB.NET項目中的MYSQL連接字符串

是的,我已經看了各種例子: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 
+0

如果你想要移動到然後,MySql開始移動所有數據訪問代碼以使用[MySql NET Connector](https://dev.mysql.com/downloads/connector/net/)並放棄OleDb。這不是一項重大的任務,只是一個重複的任務 – Steve

+0

我想要做什麼?就像我在我的問題中所說的:由於各種原因,我有各種各樣的連接,只是想保持至少重寫 – BarclayVision

+1

無論如何,您將需要重新測試所有200頁,所以您不妨做中等大小的重寫和增益改進的,一致的代碼庫 – FloatingKiwi

回答

1

不知道我完全理解這個問題,但我會在它採取刺傷。

下載MySQL NET連接器並添加對您的項目的引用。任何您使用OleDBConnection或OleDBCommand的地方都需要分別將其更改爲MySqlConnection和MySqlCommand。這應該允許您儘可能重用您現有的邏輯。

例如,在你的InsertClassRecord方法,你會改變這個

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

這個

Dim comm As New MySqlCommand(sql, conn) 

而且你應該能夠保持現有的邏輯

+0

我認爲這正是我正在尋找的,但是我遇到了連接字符串的問題,我嘗試使用:' '我得到的錯誤是:'Requested value'{MySQL ODBC 5.1驅動程序}'找不到.' – BarclayVision

+0

通過在web.config中添加connectionString來解決:'' – BarclayVision