[英]add record to mysql database in 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:连接必须有效且打开。 在 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
任何人都可以帮助我哪里出错了吗?
更新错误
MySql.Data.MySqlClient.MySqlException (0x80004005):您的 SQL 语法有错误; 检查与您的 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
查看 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
连接必须有效且打开
在data = command.ExecuteReader
之前添加con.Open()
您的 SQL 语法有错误
您有 8 个字段并尝试仅插入 7 个值。 缺失值distance
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.