刚才在论坛上看了一个帖子“主  题:  数据库的脆弱,请各路高手看一看。 ”
其中在很多回复的帖子中都提到在操作数据库时使用事务,我想请问如何在应用程序中使用事务?例如:我要从数据库中选择一些数据,使用如下语句:
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from Mytable');
ADOQuery1.Active := True;
如果要是使用事务的话,还需要添加什么代码?另外,对数据库的任何操作都使用事务有必要吗?性能会提高吗?使用事务有什么原则吗?

解决方案 »

  1.   

    /保存事件
    procedure TFareFram.ToolButton2Click(Sender: TObject);
    begin
      inherited;
        wwIncrementalSearch1.SetFocus;
        if   BookingState then
        begin
            if CheckData  then
            begin
                DMFreights.DataSource3.AutoEdit:=false;
                DMFreights.DataSource3.AutoEdit:=false;
                //Application.MessageBox('数据保存成功!','提示',MB_OK+MB_ICONINFORMATION);
                ToolButton2.Enabled:=false;
                ToolButton3.Enabled:=false;
                slkDBGrid1.ReadOnly:=true;
                slkDBGrid2.ReadOnly:=true;
                ToolButton6.Enabled:=false;            CanEditComponent(10,false);
                //added by jxp
                slkState := true;
            end
            else
            begin
                Application.MessageBox('单价没有没有填写!','提示',MB_OK+MB_ICONINFORMATION);
            end;
        end
        else
        begin
            Application.MessageBox('提单还处于撤载或归档状态!','提示',MB_OK+MB_ICONINFORMATION);
        end;
    end;
    一段我正在做的代码!
      

  2.   

    呵呵 主要部分没写
    function TFareFram.CheckData : Boolean;
    var
        Connection:TADOConnection;
    begin
        result:=false;
        Connection:= TADOConnection.Create(self);
        Connection:=GetConnection;
        Connection.BeginTrans;// /开始事务
        result:=true;
        try
             if DMFreights.Queryfreight.RecordCount>0 then
             begin
                with DMFreights.Queryfreight do
                begin
                    if VarIsNull(FieldValues['Unit_Price']) or VarIsNull(FieldValues['Account'])or VarIsEmpty(FieldValues['Unit_Price'])or VarIsEmpty(FieldValues['Account']) or VarIsNull(FieldValues['freight_name'])or VarIsEmpty(FieldValues['freight_name'])  then
                    begin
                        result:=false;
                        slkDBGrid1.SetFocus;
                        connection.RollbackTrans;//事务回卷
                        exit;
                    end
                    else
                    begin
                        UpdateBatch;
                        refresh;
                    end;
                end;
             end
             else
             begin
                with DMFreights.Queryfreight do
                begin
                    UpdateBatch;
                    refresh;
                end;
             end;        if   Panel1.Visible= true then
            begin
                if DMFreights.Queryfreight1.RecordCount>0 then
                begin
                    with DMFreights.Queryfreight1 do
                    begin
                        if VarIsNull(FieldValues['Unit_Price']) or VarIsNull(FieldValues['Account'])or VarIsEmpty(FieldValues['Unit_Price'])or VarIsEmpty(FieldValues['Account']) or VarIsNull(FieldValues['freight_name'])or VarIsEmpty(FieldValues['freight_name']) then
                        begin
                            result:=false;
                            slkDBGrid2.SetFocus;
                            connection.RollbackTrans;//事务回卷
                            exit;
                        end
                        else
                        begin
                            UpdateBatch;
                            Refresh ;
                        end;
                    end;
                end
                else
                begin
                    with DMFreights.Queryfreight1 do
                    begin
                        UpdateBatch;
                        refresh;
                    end;
                end;     end;
        Connection.CommitTrans;
        except
            Connection.RollbackTrans;
        end;end;
      

  3.   

    starttransaction
    commit
    rollback