我目前正在使用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
结束班
答案 0 :(得分:2)
您需要下载并引用 MySQL .NET Connector http://dev.mysql.com/downloads/connector/net/。然后,您将使用System.Data.MySqlClient
命名空间并将SqlConnection
和SqlCommand
替换为MySqlConnection
和MySqlCommand