BarclayVision BarclayVision - 3 months ago 21
Vb.net Question

MYSQL connection string in old VB.NET project in Access

I have a very old project that uses an Access DB (.mdb) and uses various connections from various pages. Some include OLE DB, DAO, ADO. I have over 200 pages with various connections. I'm moving over to MySQL and want to cleanup this mess. Starting with OLEDB I'm having trouble with a connection that will allow me to keep the rest of my code (or even if it can be done?)

Yes I have looked at the various examples in: http://www.connectionstrings.com/net-framework-data-provider-for-ole-db/

Here is one of the many pages I need to move to MySQL connection:

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

Answer

Not sure I fully understand the question, but I'll take a stab at it.

Download the MySQL NET Connector and add a reference to your project. Any place where you are using a OleDBConnection or OleDBCommand you will need to change that to MySqlConnection and MySqlCommand respectively. This should allow you to reuse you existing logic as much as possible.

For example, in your InsertClassRecord method you would change this

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

to this

Dim comm As New MySqlCommand(sql, conn)

And you should be able to keep the existing logic

Comments