[英]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. 我有一个非常老的项目,该项目使用Access DB(.mdb)并使用来自各个页面的各种连接。 Some include OLE DB, DAO, ADO.
其中一些包括OLE DB,DAO,ADO。 I have over 200 pages with various connections.
我有超过200页的各种连接。 I'm moving over to MySQL and want to cleanup this mess.
我将移至MySQL,并希望清理此混乱情况。 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?)
从OLEDB开始,我在连接方面遇到了麻烦,该连接将使我可以保留其余代码(即使可以完成?)
Yes I have looked at the various examples in: http://www.connectionstrings.com/net-framework-data-provider-for-ole-db/ 是的,我已经查看了以下示例: 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: 这是我转到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
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. 下载MySQL NET连接器,并添加对您的项目的引用。 Any place where you are using a OleDBConnection or OleDBCommand you will need to change that to MySqlConnection and MySqlCommand respectively.
在使用OleDBConnection或OleDBCommand的任何地方,都需要分别将其更改为MySqlConnection和MySqlCommand。 This should allow you to reuse you existing logic as much as possible.
这样可以使您尽可能地重用现有逻辑。
For example, in your InsertClassRecord method you would change this 例如,在您的InsertClassRecord方法中,您可以更改此设置
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 而且您应该能够保留现有逻辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.