日期:2014-05-17  浏览次数:20529 次

各位师兄,帮帮忙!
我想;批量更新 成绩 ,但运行时提示 update语句语法错误 数据库中成绩字段为int型,学号为字符型,课程代码为int型
protected void Button1_Click(object sender, EventArgs e)
  {
  int rowCount = GridView1.Rows.Count;

  OleDbCommand comm;
  for (int i = 0; i < rowCount; i++)
  {
  connection = new OleDbConnection(ConnStr);
  GridViewRow row = GridView1.Rows[i];
  string StuID = row.Cells[1].Text;
  string ClassID = row.Cells[2].Text;
  TextBox tb = (TextBox)row.Cells[3].FindControl("cj");
  string score = tb.Text.ToString();
  comm = new OleDbCommand("update score2 set 成绩=" + score + " where 学号='" + StuID + "' and 课程代码="+ ClassID +"", connection);
  connection.Open();
  comm.ExecuteNonQuery();
  connection.Close();
  }

------解决方案--------------------
comm = new OleDbCommand("update score2 set 成绩=" + int.Parse(score) + " where 学号='" + StuID + "' and 课程代码="+ int.Parse(ClassID), connection);

------解决方案--------------------
首先调试score StuID ClassID 是否取到了值?
另外 sql语句改成如下试试
comm = new OleDbCommand("update score2 set [成绩]=" + Convert.ToInt32(score) + " where [学号]='" + StuID + "' and [课程代码]="+ Convert.ToInt32(ClassID), connection);