简体   繁体   English

在vb.net中向mysql数据库添加记录

[英]add record to mysql database in vb.net

I am trying to add record of entered inputs to data table but it's not working.我正在尝试将输入的输入记录添加到数据表中,但它不起作用。 I have tried this.我试过这个。

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

Error错误

System.InvalidOperationException: Connection must be valid and open. 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() at index.confirmBook_Click(Object sender, EventArgs e) in E:\\MY WEB\\Quista\\Website\\index.aspx.vb:line 58在 MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) 在 MySql.Data.MySqlClient.MySqlCommand.CheckState() 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为) 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader () at index.confirmBook_Click(Object sender, EventArgs e) in E:\\MY WEB\\Quista\\Website\\index.aspx.vb:line 58

Can any one help me where I am going wrong?任何人都可以帮助我哪里出错了吗?

UPDATED ERROR更新错误

MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; MySql.Data.MySqlClient.MySqlException (0x80004005):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''email', 'contact', 'source', 'destination', 'duration', 'distance', 'dateTime',' at line 1 at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at index.confirmBook_Click(Object sender, EventArgs e) in E:\\MY WEB\\Quista\\Website\\index.aspx.vb:line 59检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“电子邮件”、“联系人”、“来源”、“目的地”、“持续时间”、“距离”、“日期时间”附近使用正确的语法在 MySql.Data.MySqlClient.MySqlStream.ReadPacket() 在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&fluenceRow, Int64&insertId) 在 MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&fluenceRows, Int64&insertId) 在MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand .ExecuteReader() at index.confirmBook_Click(Object sender, EventArgs e) in E:\\MY WEB\\Quista\\Website\\index.aspx.vb:line 59

Looking at the SQL query:查看 SQL 查询:

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

I could already see issues:我已经可以看到问题了:

  • You are quoting the field name, that is syntax error.您引用了字段名称,即语法错误。
  • You are not using parametrized queries, that is a security issue but could also be a syntax error as well.您没有使用参数化查询,这是一个安全问题,但也可能是语法错误。 for example destination would need quote if it was a string.例如,如果目的地是字符串,则需要引用。 That is the reason you need to use parameters.这就是您需要使用参数的原因。

More things:更多的东西:

  • Connection was never opened连接从未打开
  • data = command.ExecuteReader makes no sense because it is a INSERT there is nothing to read you should be using command.ExecuteNonQuery() data = command.ExecuteReader没有意义,因为它是一个INSERT没有什么可读的你应该使用command.ExecuteNonQuery()

The more I look at your code, the more issues I find, I rewrote it for you:看你的代码越多,发现的问题越多,我为你重写:

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

Connection must be valid and open连接必须有效且打开

Add con.Open() before data = command.ExecuteReaderdata = command.ExecuteReader之前添加con.Open()

You have an error in your SQL syntax您的 SQL 语法有错误

You have 8 fields and try insert only 7 values.您有 8 个字段并尝试仅插入 7 个值。 Missing value distance .缺失值distance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM