使用先插入的数据在第二个表中插入数据

时间:2015-01-31 09:52:08

标签: asp.net razor sql-server-ce

我尝试使用表单来收集数据并将其存储在表中,然后立即对该数据进行一些计算并将结果存储在单独的表中。

但我似乎无法在插入后立即阅读第一张表格。

我的计算中总是遇到零,这意味着第一个表在尝试计算时被认为是空的(但在页面刷新时不是空的,这意味着数据终于找到了通往第一个表的路)。

这是一个例子:

@{
// this is the code that gathers data from a form and stores the data in a table
 foreach(var row in db.Query(selectquestions))
 {
    var questionid = row.QuestionId;
    var answer = Request["Q" + row.QuestionId.ToString()];
    var insertanswers = "INSERT into Answers (UserId,QuestionId,Answer,ExamId) VALUES(@0, @1, @2, @3)";
    db.Execute(insertanswers,userid,questionid,answer,1);
  }

  // this is the code that tries to immediately use the data stored by the above statement and store the new calculation results in a separate table
  int totalquestions= db.Query( some query using the first table ).Count();
  int correctanswers= db.Query( some query using the first table ).Count();
  int incorrectanswers=db.Query( some query using the first table ).Count();

  var score= (float)(correctanswers * 3 - incorrectanswers) / (totalquestions * 3)*100;

  var insertscores = "INSERT into Scores (UserId,ExamId,ItemId,Score) VALUES(@0, @1, @2, @3)"; 
  db.Execute(insertscores,userid,1,1,score.ToString());
}

我正在使用网页/ razor / SQL Server CE ..任何建议都将不胜感激

0 个答案:

没有答案