检查与您的mysql服务器版本对应的手册,以便在'附近使用正确的语法(CourseId = 'mect2

本文关键字:mect2 CourseId 语法 服务器 版本 mysql 检查 | 更新日期: 2023-09-27 18:08:06

我正在用c#编写一个使用MySql的学生数据库程序。我想更新信息,但它总是给我那个错误。这是我写的程序。

private void Update_bttn_Click(object sender, EventArgs e)
{
    string ConString = " datasource = localhost; port = 3306; username = root; password = 3306";
    string Query = " Update studentdata.studentrecord set (CourseId = '" + this.crsId.Text + "', CourseName = '" + this.crsName.Text + "',Credits = '" + this.credits.Text + "', CourseStatement = '" + this.CrseStatment.Text + "',Grade = '" + this.Grades.Text + "' where CourseId = '" + this.crsId.Text+"' ; ";
    MySqlConnection ConDatabase = new MySqlConnection(ConString);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, ConDatabase);
    MySqlDataReader myReader;
    try
    {
        ConDatabase.Open();
        myReader = cmdDataBase.ExecuteReader();
        MessageBox.Show("Information Updated");
        while ((myReader.Read())) { }
        ConDatabase.Close();
    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }
}

检查与您的mysql服务器版本对应的手册,以便在'附近使用正确的语法(CourseId = 'mect2

使用参数化查询避免SQL注入

SQLParameter如何防止SQL注入?

(靠近CourseId

string query = update studentdata.studentrecord set CourseId =@CourseId,CourseName=@CourseName,Credits =@Credits,CourseStatement=@CourseStatement,Grade =@Grade  where CourseId =@CourseId";
MySqlConnection ConDatabase = new MySqlConnection(ConString);
MySqlCommand cmdDataBase = new MySqlCommand(query, ConDatabase);
cmdDataBase.Parameters.AddWithValue("@CourseId",this.crsId.Text );
cmdDataBase.Parameters.AddWithValue("@CourseName", this.crsName.Text);

等等

为将来参考,您可以通过调试获取query的值,并尝试在SQL中运行实际查询以获得更精确的错误。然而,在这种情况下,这是因为您在字符串中有一个开括号(CourseId...而没有关闭)

另外,您可能需要查看有关c#中Sql类使用的一些文档