【发布时间】:2014-08-01 20:28:25
【问题描述】:
我正在使用 MySql 在 C# 上编写一个学生数据库程序。我想更新信息,但它总是给我那个错误。这是我写的程序。
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); }
}
【问题讨论】:
-
您正在打开一个括号
(CourseId,但没有在代码中的任何位置关闭它。不要连接查询,使用参数。您当前的代码容易发生 SQL 注入。 -
@Habib,这就是答案。
-
我已经放了 )" 但仍然是相同的输出:还有其他建议吗?
-
在执行之前使用调试器获取
Querry的实际值。获取它的值并尝试直接对您的数据库执行该命令。那至少应该给你一个更详细的错误
标签: c# mysql syntax-error