解析查询时出错。 [令牌行号= 1,令牌行偏移= 229,令牌错误=交互式]

时间:2014-03-21 07:47:49

标签: c#

尝试执行get error

cmd.CommandText = "insert into Finalresult (section_name,examid,userid,solveqty,dateexam,totalqty) values('" + section + "','" + examis + "','" + UserId + "','" + newId + "','" + DateTime.Now + "'"+
                ",'(Select count(SubQno) from Question where Section_name='" + section + "')')";

            cmd.ExecuteNonQuery();

在cmd.ExecuteNonQuery()行获取错误;错误消息是解析查询时出错。 [令牌行号= 1,令牌行偏移= 229,令牌错误=交互式]

1 个答案:

答案 0 :(得分:1)

嵌套选择周围不需要单引号。

cmd.CommandText = "insert into Finalresult (section_name,examid,userid,solveqty,dateexam,totalqty) values('" + section + "','" + examis + "','" + UserId + "','" + newId + "','" + DateTime.Now + "',(Select count(SubQno) from Question where Section_name='" + section + "'))";

cmd.ExecuteNonQuery();

你不应该这样做,你应该使用command parameters来建立你的查询。

e.g。

cmd.CommandText = "insert into Finalresult (section_name,examid,userid,solveqty,dateexam,totalqty) values(@section, @exam, @user, @solve, @date, (Select count(SubQno) from Question where Section_name=@section))";

cmd.Parameters.AddWithValue("@section", section);