是什么导致了这个MySql.Data.MySqlClient.MySqlException?C#.

本文关键字:MySqlClient Data MySqlException MySql 是什么 | 更新日期: 2023-09-27 18:34:18

我已经浏览了一段时间的代码,但我看不到任何会导致cmd.ExecuteNonQuery()出现以下错误的内容:

MySql.Data.dll 中发生了类型为"MySql.Data.MySqlClient.MySqlException"的未处理异常其他信息:您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的"附近使用的正确语法

这是我的代码:

public void InsertReservation(Reservation reservation)
{ //To counter the unavailable Auto Increment in the database
    int ReservationID = 0;
    string query = "SELECT max(ReservationID) FROM Reservation";
    if (this.OpenConnection())
    {
    MySqlCommand cmd = new MySqlCommand(query, connection);
    ReservationID = (Int32)cmd.ExecuteScalar();
    CloseConnection();
    }
    ReservationID++;
    string query2 = "INSERT INTO `Reservation`(`ReservationID`, `CarID`, `CustomerID`, `Startdate`, `Enddate`, `Confirmed`, `Kilometres`, `Pickupcity`, `Pickupstreetname`, `Pickupstreetnumber`, `Pickupstreetnumbersuffix`, `Paid`, `Comment`) VALUES(@reservationid,@carid,@customerid,@startdate,@enddate,@confirmed,@Kilometres,@pickupcity,@pickupstreetname,@pickupnumber,@pickupnumbersuffix,@paid,@comment";
    if (this.OpenConnection())
    {
        int convertCofnfirmedToInt;
        int convertPaidtoInt;
        MySqlCommand cmd = new MySqlCommand(query2, connection);
        cmd.Parameters.AddWithValue("@reservationid", ReservationID);
        cmd.Parameters.AddWithValue("@carid", reservation.carID);
        cmd.Parameters.AddWithValue("@customerid", reservation.customerID);
        cmd.Parameters.AddWithValue("@startdate", reservation.startdate);
        cmd.Parameters.AddWithValue("@enddate", reservation.enddate);
        cmd.Parameters.AddWithValue("@confirmed", reservation.);
        cmd.Parameters.AddWithValue("@Kilometres", reservation.kilometres);
        cmd.Parameters.AddWithValue("@pickupcity", reservation.pickupcity);
        cmd.Parameters.AddWithValue("@pickupstreetname", reservation.pickupstreetname);
        cmd.Parameters.AddWithValue("@pickupnumber", reservation.pickupstreetnumber);
        cmd.Parameters.AddWithValue("@pickupnumbersuffix", reservation.pickupstreetnumbersuffix);
        cmd.Parameters.AddWithValue("@paid", paidbool);
        cmd.Parameters.AddWithValue("@comment", reservation.comment);

        cmd.ExecuteNonQuery();
        this.CloseConnection();
    }
}    

如果有人能帮助我,那就太好了!

是什么导致了这个MySql.Data.MySqlClient.MySqlException?C#.

在 sql INSERT 的末尾添加右圆括号)

string query2 = "INSERT INTO `Reservation`(`ReservationID`, `CarID`, `CustomerID`, `Startdate`, `Enddate`, `Confirmed`, `Kilometres`, `Pickupcity`, `Pickupstreetname`, `Pickupstreetnumber`, `Pickupstreetnumbersuffix`, `Paid`, `Comment`) VALUES(@reservationid,@carid,@customerid,@startdate,@enddate,@confirmed,@Kilometres,@pickupcity,@pickupstreetname,@pickupnumber,@pickupnumbersuffix,@paid,@comment)";
相关文章: