我目前正在开发一个使用ASP.NET
的网页,VB
作为后端。但我面临着一个问题。我不知道如何建立数据库连接以连接MYSQL WORKBENCH
.我只是写了连接,但它只是为SQL server
编码。
这是我用于连接的示例代码,但它用于sql server
.我只需要mySQL
工作台的代码。
Imports System.Data
Imports System.Data.SqlClient
Partial Class Request
Inherits System.Web.UI.Page
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
txt1.Focus()
txt2.Focus()
txt3.Focus()
txt4.Focus()
txt5.Focus()
txt6.Focus()
txt7.Focus()
ddl1.Focus()
ddl2.Focus()
ddl3.Focus()
ddl4.Focus()
End Sub
Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
con.ConnectionString = "Server=localhost;Database=test;Uid=root;Pwd=1234;"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO cr_record (Emplid, Nama, date, DeptDesc, email, Change, reasonchange,problem,priority,reasondescription,systemrequest) VALUES (@Emplid, @Nama, @date, @DeptDesc, @email, @Change, @reasonchange,@problem,@priority,@reasondescription,@systemrequest)"
cmd.Parameters.AddWithValue("@Emplid", ddl1.Text)
cmd.Parameters.AddWithValue("@Nama", TextBox1.Text)
cmd.Parameters.AddWithValue("@date", txt5.Text)
cmd.Parameters.AddWithValue("@DeptDesc", txt2.Text)
cmd.Parameters.AddWithValue("@email", txt4.Text)
cmd.Parameters.AddWithValue("@Change", ddl2.Text)
cmd.Parameters.AddWithValue("@reasonchange", txt6.Text)
cmd.Parameters.AddWithValue("@problem", ddl3.Text)
cmd.Parameters.AddWithValue("@priority", rbl1.Text)
cmd.Parameters.AddWithValue("@reasondescription", txt7.Text)
cmd.Parameters.AddWithValue("@systemrequest", ddl4.Text)
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Added Successfully")
End Sub
结束类
您需要
下载并引用MySQL .NET Connector http://dev.mysql.com/downloads/connector/net/。然后,您将使用 System.Data.MySqlClient
命名空间,并将SqlConnection
和SqlCommand
替换为 MySqlConnection
和 MySqlCommand