VB.connection字符串后面的代码?

时间:2014-03-04 08:39:31

标签: mysql asp.net vb.net connection-string mysql-workbench

我目前正在使用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

结束班

1 个答案:

答案 0 :(得分:2)

您需要下载并引用 MySQL .NET Connector http://dev.mysql.com/downloads/connector/net/。然后,您将使用System.Data.MySqlClient命名空间并将SqlConnectionSqlCommand替换为MySqlConnectionMySqlCommand