简体   繁体   English

一起使用INNER JOIN和插入和更新时在VB.Net中出错

[英]Getting error in VB.Net when using INNER JOIN with insert and update together

I'm trying to INSERT and UPDATE using INNER JOIN but whenever I save it gives me an error around SET I can't figure it out what it is really wrong with it it's my first time using INNER JOIN I really need help for this 我正在尝试使用INNER JOIN进行INSERT和UPDATE,但是每当我保存它时,都会在SET周围出现错误,我无法弄清它到底有什么问题,这是我第一次使用INNER JOIN,我确实为此需要帮助

mysqlConn.Open()
sqlCmd = New Odbc.OdbcCommand
sqlCmd.Connection = mysqlConn

sqlCmd.CommandText = "INSERT INTO customer (guestno, datein, dateout, fullname, address, contactnum, total, paid, balance) VALUES ('" & Replace(Trim(staff_ri_txtguestno.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_dtpin.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_dtpout.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_txtfullname.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_txtaddress.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_txtnumber.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_txttotal.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_txtpaid.Text), "'", "''") & "', '" & Replace(Trim(staff_ri_txtbalance.Text), "'", "''") & "')
SET room.customer_id = customer.customer_id
UPDATE room SET roomstatus = '" & Replace(Trim("Occupied"), "'", "''") & "'
FROM room T1 INNER Join customer T2 ON T1.customer_id = T2.customer_id
WHERE room_id ='" & staff_lblid.Text & "'"
dr = sqlCmd.ExecuteReader

mysqlConn.Close()

My database structure 我的数据库结构

Table name: room 表名:房间

room_id | room_id | roomnum | 室号| roomstatus | roomstatus | roomtype | 房间类型| rate | 率| customer_id 顾客ID

Table name: customer 表名:客户

customer_id | customer_id | guestno | guestno | datein | datein | dateout | 过期| fullname | 全名| address | 地址| contactnum | contactnum | total | 总计 paid | 付费| balance 平衡

Here's the error Screenshot 这是错误截图

Breaking down your code and Sql statement, what you are asking MySql to do is: 分解代码和Sql语句,您要MySql做的是:

  1. INSERT new customer ... - This looks OK 插入新客户...-看起来不错
  2. SET Customer Id on the Room table ... - This is not valid Sql 在会议室表上设置客户ID ...-这是无效的Sql
  3. UPDATE Room status ... - This looks OK. 更新房间状态...-看起来不错。

Couple this with how your generating your Sql statements it is understandable that you are having issues. 将此与您如何生成Sql语句结合在一起,可以理解您遇到了问题。

What I did was to rewrite your Sql using Parameters, which will help prevent many issues now and in the future, and also break down the changes to the Customer and Room tables in to two separate Sql statements that are executed at the same time. 我所做的就是使用Parameters重写了Sql,这将有助于防止现在和将来出现许多问题,并且还将对CustomerRoom表的更改分解为同时执行的两个单独的Sql语句。

Using conn As New MySqlConnection(ConnectionString)
    Dim insertCustomerSql As String = String.Empty
    insertCustomerSql += "INSERT INTO CUSTOMER (guestno, datein, dateout, name, address, contactnum, total, paid, balance) "
    insertCustomerSql += "VALUES (@guestNo, @dateIn, @dateOut, @name, @address, @contactNum, @total, @paid, @balance);"

    Dim updateRoomSql As String = String.Empty
    updateRoomSql += "UPDATE ROOM SET roomstatus = @roomStatus, customerid = LAST_INSERT_ID() WHERE id = @roomId;"

    Dim sql As String = insertCustomerSql + Environment.NewLine + updateRoomSql

    conn.Open()

    Using command As New MySqlCommand(sql, conn)
        command.Parameters.Add("guestNo", MySqlDbType.String).Value = 101
        command.Parameters.Add("dateIn", MySqlDbType.Date).Value = DateTime.Now.Date
        command.Parameters.Add("dateOut", MySqlDbType.Date).Value = DateTime.Now.Date.AddDays(5)
        command.Parameters.Add("name", MySqlDbType.String).Value = "JayV"
        command.Parameters.Add("address", MySqlDbType.String).Value = "My Home, England"
        command.Parameters.Add("contactNum", MySqlDbType.String).Value = "+44 1234 5678"
        command.Parameters.Add("total", MySqlDbType.Decimal).Value = 500
        command.Parameters.Add("paid", MySqlDbType.Decimal).Value = 350
        command.Parameters.Add("balance", MySqlDbType.Decimal).Value = 150

        command.Parameters.Add("roomStatus", MySqlDbType.String).Value = "Occupied"
        command.Parameters.Add("roomId", MySqlDbType.Int16).Value = 1
        command.ExecuteNonQuery()
    End Using
End Using

I also took the opportunity to make use of some other good practices, like the Using blocks. 我还借此机会利用了其他一些良好的做法,例如Using块。

Hope this helps you. 希望这对您有所帮助。

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

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