导致此MySql.Data.MySqlClient.MySqlException的原因是什么? C#

时间:2014-12-15 11:59:30

标签: c# database exception mysql

我已经查看了我的代码一段时间了,我看不到任何会导致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();
    }

}    

如果有人可以帮助我,那就太棒了!

1 个答案:

答案 0 :(得分:2)

在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)";