将记录添加到vb.net中的mysql数据库

时间:2015-11-10 12:27:15

标签: mysql asp.net vb.net

我正在尝试将输入的输入记录添加到数据表中,但它无法正常工作。我试过这个。

vb.net

Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient

Partial Class index
    Inherits System.Web.UI.Page
    Dim con As New MySqlConnection("Data Source=204.11.58.166;port=3306;Initial Catalog=quistaBooking;User Id=my_username;password=my_password ;")

Protected Sub confirmBook_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles confirmBook.Click

            Dim emailID, contactNo, source, destination, duration, distance, fare, datetime, vehicle As String

            emailID = email.Text
            contactNo = contact.Text
            source = txtSource.Text
            destination = txtDestination.Text
            duration = dvDuration.Text
            distance = dvDistance.Text
            datetime = datetimepicker.Text
            vehicle = selectVehicle.SelectedItem.ToString

        Try
            Dim str1 As String = "INSERT INTO logistics ('email', 'contact', 'source', 'destination', 'duration', 'distance', 'dateTime', 'vehicleType') values ('" + emailID + "', '" + contactNo + "', '" + source + "', '" + destination + "', '" + duration + "', '" + datetime + "', '" + vehicle + "')"
            Dim data As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = str1
            command.Connection = con
            adapter.SelectCommand = command
            data = command.ExecuteReader
            con.Close()
        Catch ex As Exception
            Response.Write(ex)
        End Try
    End Sub
End Class

错误

System.InvalidOperationException:连接必须有效且已打开。 at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)at MySql.Data.MySqlClient.MySqlCommand.CheckState()at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader ()位于E:\ MY WEB \ Quista \ Website \ index.aspx.vb中的index.confirmBook_Click(Object sender,EventArgs e):第58行

任何人都可以在我出错的地方帮助我吗?

更新错误

MySql.Data.MySqlClient.MySqlException(0x80004005):您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#"'电子邮件','联系',' source',&#附近使用正确的语法39;目的地','持续时间','距离',' dateTime','在在MySql.Data.MySqlClient.MySqlStream.ReadPacket()在MySql.Data.MySqlClient.NativeDriver.GetResult(的Int32&安培; affectedRow,Int64的&安培; insertedId)线1在MySql.Data.MySqlClient.Driver.GetResult(的Int32 statementId,的Int32&安培; affectedRows,Int64的&安培;在MySql.Data.MySqlClient.Driver.NextResult(的Int32 statementId,布尔力)在MySql.Data.MySqlClient.MySqlDataReader.NextResult()insertedId)在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(的CommandBehavior行为)在位于E:\ MY WEB \ Quista \ Website \ index.aspx.vb:第59行的index.confirmBook_Click(Object sender,EventArgs e)中的MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()

2 个答案:

答案 0 :(得分:1)

查看SQL查询:

"INSERT INTO logistics 
('email', 'contact', 'source', 'destination', 'duration', 'distance', 'dateTime', 'vehicleType')
values ('" + emailID + "', '" + contactNo + "', '" + source + "', '" + destination + "', '" + duration + "', '" + datetime + "', '" + vehicle + "')"

我已经看到了问题:

  • 您正在引用字段名称,即语法错误。
  • 您没有使用参数化查询,这是一个安全问题,但也可能是语法错误。例如,如果目标是字符串,则需要引用。这就是你需要使用参数的原因。

更多事情:

  • 从未打开过连接
  • data = command.ExecuteReader毫无意义,因为它是INSERT没有什么可读的,你应该使用command.ExecuteNonQuery()

我看你的代码越多,我发现的问题就越多,我就为你重写了它:

Try
   con.Open()
   command.Connection = con

   command.CommandText = "INSERT INTO logistics (`email`, `contact`, `source`, `destination`, `duration`, `distance`, `dateTime`, `vehicleType`) values  (@emailID, @contactNo, @source, @destination, @duration, @datetime, @vehicle)"
   command.Prepare()
   command.Parameters.AddWithValue("@emailID", emailID)
   command.Parameters.AddWithValue("@contactNo", contactNo)
   command.Parameters.AddWithValue("@source", source)
   command.Parameters.AddWithValue("@destination", destination)
   command.Parameters.AddWithValue("@duration", duration)
   command.Parameters.AddWithValue("@datetime", datetime)
   command.Parameters.AddWithValue("@vehicle", vehicle)
   command.Parameters.AddWithValue("@text", "One")
   command.ExecuteNonQuery()
Catch ex As MySqlException
    MessageBox.Show("Error " & ex.Number & " has occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

答案 1 :(得分:0)

  

连接必须有效并且打开

con.Open()

之前添加data = command.ExecuteReader
  

您的SQL语法错误

您有8个字段,并尝试仅插入7个值。缺少值distance