将INNER JOIN与插入和更新一起使用时,在VB.Net中出现错误

时间:2019-05-04 19:54:28

标签: mysql .net vb.net

我正在尝试使用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()

我的数据库结构

表名:房间

room_id |室号| roomstatus |房间类型|率| customer_id

表名:客户

customer_id | guestno | datein |过期|全名|地址| contactnum |总计付费|平衡

Here's the error Screenshot

1 个答案:

答案 0 :(得分:2)

破坏代码和Sql语句,您要求MySql做的是:

  1. 插入新客户...-看起来不错
  2. 在“房间”表上设置客户ID ...-这是无效的Sql
  3. 更新房间状态...-看起来不错。

将此与您生成Sql语句的方式相结合,可以理解您遇到了问题。

我所做的是使用参数重写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

我还借此机会利用了其他一些好的做法,例如Using块。

希望这对您有所帮助。