如何在Delphi 7中运行更新查询时解决闭合数据集错误?

时间:2013-11-04 08:20:38

标签: sql delphi ms-access delphi-7 ado

我在使用Microsoft Jet引擎和Microsoft Access(* .mdb)数据库的应用程序中继续在Delphi 7中出错。我通过TADOQuery组件建立连接。该错误表示“无法对已关闭的数据集执行此操作”'并且在尝试执行UPDATE查询时仅在一个事件处理程序中发生:

frmHome.adoqryMain.SQL.Text := 'UPDATE Predictions SET Complete = True WHERE UID = "'+sUID+'"';

事件处理程序的代码如下:

    procedure TfrmAdmin.bmbSubmitClick(Sender: TObject);
var
  sScore, sEID, sPrediction, sUID : String;
  iRecordCount, x, iPos, iLength, iActual, iPoints, iPointsNew : Integer;
  rPrediction : Real;
begin
  // Assign values to variables
  sScore := IntToStr(sedSuthies.Value) + '-' + IntToStr(sedOpponent.Value);
  iActual := sedSuthies.Value + sedOpponent.Value;
  sEID := frmHome.arrEID[lstEvents.ItemIndex];
  // Update the score for the event in the database
  frmHome.adoqryMain.Active := False;
  frmHome.adoqryMain.SQL.Clear;
  frmHome.adoqryMain.SQL.Text := 'UPDATE Events SET Score = "'+sScore+'",Complete = True WHERE EID = "'+sEID+'" ';
  frmHome.adoqryMain.ExecSQL;
  frmHome.adoqryMain.SQL.Text := 'SELECT * FROM Predictions WHERE EID = "'+sEID+'" ';
  frmHome.adoqryMain.Open;
  iRecordCount := frmHome.adoqryMain.RecordCount;
  //Assign points to users for all the predictions
  for x := 0 to (iRecordCount - 1) do begin
    sUID := frmHome.adoqryMain.Fields[1].AsString;
    sPrediction := frmHome.adoqryMain.Fields[4].AsString;
    iPos := Pos('-',sPrediction) - 1;
    iLength := Length(sPrediction) - iPos;
    ShowMessage('1');
    if ((sedSuthies.Value >= sedOpponent.Value) AND (StrToFloat(Copy(sPrediction, 1, iPos)) >= StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)))) OR ((sedSuthies.Value < sedOpponent.Value) AND (StrToFloat(Copy(sPrediction, 1, iPos)) < StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)))) then begin
      rPrediction := StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)) + StrToFloat(Copy(sPrediction, 1, iPos));
      if rPrediction >= iActual then
        rPrediction := rPrediction - iActual
      else
        rPrediction := iActual - rPrediction;
      iPoints := Round(10 * (1 - (rPrediction / iActual)));
    end
    else
      iPoints := 0;
    ShowMessage('2');
    frmHome.adoqryMain.Open;
    frmHome.adoqryMain.SQL.Text := 'UPDATE Predictions SET Complete = True WHERE UID = "'+sUID+'"';
    frmHome.adoqryMain.ExecSQL;
    ShowMessage('3');
    ShowMessage(IntToStr(iPoints));
    frmHome.adoqryMain.Next;
  end;
  ShowMessage('Score succefully submitted!');
  frmHome.UpdateEventLists;
end;

此刻它有点乱,但无论UPDATE查询更新什么,它总是会出现此错误。我尝试打开TADOQuery,使用各种运行查询的方法(带语句和参数)等。任何帮助都将不胜感激。

P.S。 ShowMessage函数用于调试目的,将被删除。

1 个答案:

答案 0 :(得分:3)

我想我知道你要做什么。您使用相同的qry来从中获取数据,并在循环中使用它来更新数据库。

开始使用2个adoqueries然后它应该可以工作。

所以Adoquerymain和一个新的AdoQueryInLoop

ShowMessage('2');
frmHome.adoqryInLoop.Close; //I'm an overzealous query closer
frmHome.adoqryInLoop.SQL.Text := 'UPDATE Predictions SET Complete = True WHERE UID = "'+sUID+'"';
frmHome.adoqryInLoop.ExecSQL;
ShowMessage('3');