我做了个单机版库存管理,ADO连接,所有出库/入库/更新都通过一个Query用SQL完成。但是莫名其妙的出现数据丢失现象---所有操作正常,不定时出现关闭程序后输入的大部分丢失,并似乎输入出库数据多时易发。跟踪没有发现异常。
我用win2k+Access2k,客户平台是win98+Access2k,在客户机其商业调试编译过。
请指教:(

解决方案 »

  1.   

    用事务保护操作成功:
    (**) if database1.InTransaction  then
           database1.rollback; frmd.database1.StartTransaction ;try
         database1.StartTransaction;
         //你的数据提交代码
         database1.commit;
    except 
         database1.rollback;
    end;
      

  2.   

    function TFrmMain.Save: Boolean;
    begin
      try
        dm.aCnn.BeginTrans;
        SavetoSales;  //先更新销售主表
        with dm.aQrGen do    //更新销售详表
        begin
          if active then close;
          sql.Clear;
          sql.Add('insert into saledetails (sid,pname,sqty,sprice,pprice,re)'
                  + ' values(' + ''''+edtsid.Text+'''' +','+''''+edtpname.Text+''''
                  +',' + edtsqty.Text+','+ edtsprice.Text + ',' + edtprice.Text
                  +','+''''+SEstr+''''+ ')');
          ExecSql;
        end;
        dm.aCnn.CommitTrans;
        ClearEdtSales;
        RefreshGrid(dm.aTabSDetail);
        RefreshGrid(dm.aTabSales);
        Result := true;
      except
        begin
          dm.aCnn.RollbackTrans;
          raise exception.Create('保存错误!请检查输入重试!');
        end;
      end;
    end;
    function FrmMain.SavetoSales: boolean;
    with dm.aQrSales do
      begin
        if active then close;
        sql.Clear;
        sql.Add('select * from sales where sid='+''''+edtsid.Text+'''');
        open;
        if recordcount <> 0 then
          SQLstr := 'update sales set mqty=mqty+'+edtsqty.Text+',maccount=maccount+'
                    +floattostr(strtofloat(edtsqty.Text)*strtofloat(edtsprice.Text))
                    +' Where sid='+''''+EdtSID.text+''''
        else
          SQLstr :='insert into sales(sid,mqty,maccount,sdate,suser,customer,tel)'
                +' values('+''''+edtsid.Text+''''+','+edtsqty.Text+','
                +floattostr((strtofloat(edtsprice.Text))*(strtofloat(edtsqty.Text)))
                +',#'+edtsdate.Text+'#,'+''''+edtuser.Text+''''+','+''''
                +edtcust.Text+''''+','+''''+edttel.Text+''''+')';
        close;
        sql.Clear;
        sql.Add(sqlstr);
        ExecSQL;
        close;
        //更新产品库
        UpdatePQty(edtsqty.Text,fpid,false);
        Result := true;
      end;
    end;
      

  3.   

    这样看看可以不 ?-------------------------------------------
    function TFrmMain.Save: Boolean;
    begin
      Result := False ;
        dm.aCnn.BeginTrans;
        if  not SavetoSales then Exit ;  
        with dm.aQrGen do    //更新销售详表
        begin
          Close ; 
          sql.Clear;
          sql.Add('insert into saledetails (sid,pname,sqty,sprice,pprice,re)'
                  + ' values(' + ''''+edtsid.Text+'''' +','+''''+edtpname.Text+''''
                  +',' + edtsqty.Text+','+ edtsprice.Text + ',' + edtprice.Text
                  +','+''''+SEstr+''''+ ')');
          ExecSql;
        end;
      try 
        dm.aCnn.CommitTrans;
        Result := true;
      except
        begin
          dm.aCnn.RollbackTrans;
          raise exception.Create('保存错误!请检查输入重试!');
        end;
      end;
    end;function FrmMain.SavetoSales: boolean;
    begin
      Result := Fasle ;
    with dm.aQrSales do
      begin
        Close ;
        sql.Clear;
        sql.Add('select * from sales where sid='+''''+edtsid.Text+'''');
        open;
        if recordcount <> 0 then
          SQLstr := 'update sales set mqty=mqty+'+edtsqty.Text+',maccount=maccount+'
                    +floattostr(strtofloat(edtsqty.Text)*strtofloat(edtsprice.Text))
                    +' Where sid='+''''+EdtSID.text+''''
        else
          SQLstr :='insert into sales(sid,mqty,maccount,sdate,suser,customer,tel)'
                +' values('+''''+edtsid.Text+''''+','+edtsqty.Text+','
                +floattostr((strtofloat(edtsprice.Text))*(strtofloat(edtsqty.Text)))
                +',#'+edtsdate.Text+'#,'+''''+edtuser.Text+''''+','+''''
                +edtcust.Text+''''+','+''''+edttel.Text+''''+')';
        close;
        sql.Clear;
        sql.Add(sqlstr);
        ExecSQL;
        //更新产品库    UpdatePQty(edtsqty.Text,fpid,false); // 也是一个函数 ?    Result := true;
      end;
    end;