为什么更新查询不起作用?

时间:2014-03-19 11:18:40

标签: sql database

我正在使用SqlServerCeinsert查询工作正常,但update查询提供了例外。

这是我的代码:

SqlCeConnection _connection _connection = new SqlCeConnection(@"Data Source=MyDatabase#1.sdf;Password=xxxxx;");
_connection.Open();

cmd.Connection = _connection;
cmd.CommandText = " UPDATE [Solve_Student_question] 
                       SET Answ= '" + ans + "' ,
                           Start_time='" + sTime + "',
                           End_time='" + eTime + "' 
                     WHERE Qno='" + Qno + "' AND 
                           User_id='" + userid + "' AND
                           Exame_id='" + examid + "' sectionname='" + sectionname + "'";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

_connection.Close();

它在行cmd.ExecuteNonQuery();

处出错
An exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in 
System.Data.SqlServerCe.dll but was not handled in user code

2 个答案:

答案 0 :(得分:3)

AND之前您遗漏了sectionname。修正了以下SQL:

cmd.CommandText = " UPDATE [Solve_Student_question] 
                   SET Answ= '" + ans + "' ,
                       Start_time='" + sTime + "',
                       End_time='" + eTime + "' 
                 WHERE Qno='" + Qno + "' AND 
                       User_id='" + userid + "' AND
                       Exame_id='" + examid + "' AND sectionname='" + sectionname + "'";

答案 1 :(得分:0)

cmd.CommandText = " Update [Solve_Student_question] set Answ= '" + ans + "' ,Start_time='" + sTime + "',End_time='" + eTime + "' where Qno='" + Qno + "' AND User_id='" + userid + "' and Exame_id='" + examid + "'and sectionname='" + sectionname + "'";