unit OperationClass;interface
uses
  Forms, Windows,SysUtils,DB, ADODB,DBTables;
type
  //对数据集的操作
  ToperateDataSet=Class
  private
    FSQL_Text:String;
    //判断对数据集操作是否符合条件
    function JudgeIF:Boolean;
  protected
    procedure ExecuteAppend;
    procedure ExecuteDelete;
    procedure ExecuteModified;
  public
    //constructor Create(ADOQuy:TADOQuery;BDEQuy:TQuery;SQLText:String);
    //Destructor  Destroy;Override;
    property SQL_Text:String read FSQL_Text write FSQL_Text;
    procedure Append;
    procedure Delete;
    procedure Modified;
  end;
implementation
var
  FADOQuy:TADOQuery;
  FBDEQuy:TQuery;
  FSQLText:String;Type
   Box_Flag=(mtInformation,mtError,mtWarning);
Type
   Flag=(btOK,btOKCancel);//*================================================================
//* 函数 名:ShowMessageBox
//* 参    数:Content:string;Box_Type:Box_Flag;Flag:Flag
//*           Box_Type 1:mtWarning 2:mtError 3:mtInformation
//*           Flag 1:mb_Yes,mb_cancle
//* 功能描述: 各种提示信息(信息提示,错误提示,警告提示)
//*
//* 返 回 值:
//*
//* 抛出异常:
//*
//* 作    者:谈广才 2003/5/23
//*================================================================
Function Func_ShowMessagebox(Content:string;Box_Type:Box_Flag;Flag:Flag):Integer;
Begin
Case Box_Type Of
  mtInformation:
  Begin
     IF Flag=btOK Then
        Result:=Application.MessageBox(Pchar(Content),'提示信息',
                                       MB_OK+MB_ICONINFORMATION)
     Else
        Result:=Application.MessageBox(PChar(Content),'提示信息',
                                       MB_OKCANCEL+MB_ICONINFORMATION);
  End;
  mtError:
  Begin
     IF Flag=btOK Then
        Result:=Application.MessageBox(PChar(Content),'错误提示',
                                       MB_OK+MB_ICONERROR);
  End;
  mtWarning:
  Begin
     IF Flag=btOK Then
        Result:=Application.MessageBox(PChar(Content),'请您注意',
                                       MB_OK+MB_ICONWARNING)
     Else
        Result:=Application.MessageBox(PChar(Content),'请您注意',
                                       MB_OKCANCEL+MB_ICONWARNING);
  End;
End;
End;
{ ToperateDataSet }procedure ToperateDataSet.Append;
begin
  ExecuteAppend;
end;{constructor ToperateDataSet.Create(ADOQuy: TADOQuery; BDEQuy: TQuery;
  SQLText: String);
begin
  FADOQuy:=ADOQuy;
  FBDEQuy:=BDEQuy;
  FSQLText:=SQLText;
end; }procedure ToperateDataSet.Delete;
begin
  ExecuteDelete;
end;{destructor ToperateDataSet.Destroy;
begin
  FADOQuy:=nil;
  FBDEQuy:=nil;
  FSQLText:='';
  inherited;
end;
       }
procedure ToperateDataSet.ExecuteAppend;
begin
  if JudgeIF then
  begin
    try
       if (FADOQuy<>nil) and (FSQLText<>'')  then
       begin
          if FADOQuy.State<>dsInsert then
          begin
             with FADOQuy do
             begin
                Close;
                SQL.Clear;
                SQL.Add(FSQLText);
                Open;
                Append;
             end;
          end;
       end
       else if (FBDEQuy<>nil ) and (FSQLText<>'') then
       begin
          if FBDEQuy.State<>dsInsert then
          begin
             with FBDEQuy do
             begin
                Close;
                SQL.Clear;
                SQL.Add(FSQLText);
                Open;
                Append;
             end;
          end;
       end;
    except
       on E:Exception do
       Func_ShowMessagebox('执行增加操作出错!  错误原因如下:'+Chr(10)+E.message,mtError,btOK);
    end;
  end;end;

解决方案 »

  1.   

    procedure ToperateDataSet.ExecuteDelete;
    begin
      if JudgeIF then
      begin
         try
            if (FADOQuy<>nil) and (FSQLText<>'')  then
            begin
               with FADOQuy do
               begin
                  Close;
                  SQL.Clear;
                  SQL.Add(FSQLText);
                  Open;
                  IF Not IsEmpty Then
                  begin
                     if Func_ShowMessagebox('你确定要删除当前记录吗?',mtInformation,btOKCancel)=1 then
                        Delete ;
                  end
                  else
                     Func_ShowMessagebox('没有你要删除的记录!',mtInformation,btOK);
               end;
            end
            else if (FBDEQuy<>nil ) and (FSQLText<>'') then
            begin
               with FBDEQuy do
               begin
                  Close;
                  SQL.Clear;
                  SQL.Add(FSQLText);
                  Open;
                  IF Not IsEmpty Then
                  begin
                     if Func_ShowMessagebox('你确定要删除当前记录吗?',mtInformation,btOKCancel)=1 then
                        Delete ;
                  end
                  else
                     Func_ShowMessagebox('没有你要删除的记录!',mtInformation,btOK);
               end;
            end;
         except
            on E:Exception do
            Func_ShowMessagebox('执行删除操作出错!  错误原因如下:'+Chr(10)+E.message,mtError,btOK);
         end;
      end;end;procedure ToperateDataSet.ExecuteModified;
    begin
      if JudgeIF then
      begin
         try
            if (FADOQuy<>nil) and (FSQLText<>'')  then
            begin
               if FADOQuy.State<>dsEdit then
               begin
                  with FADOQuy do
                  begin
                     Close;
                     SQL.Clear;
                     SQL.Add(FSQLText);
                     Open;
                     IF Not IsEmpty Then
                        Edit
                     else
                        Func_ShowMessagebox('没有你要修改的记录!',mtInformation,btOK);
                  end;
               end
               else
                  Func_ShowMessagebox('数据集已经是修改状态!',mtInformation,btOK);
            end
            else if (FBDEQuy<>nil ) and (FSQLText<>'') then
            begin
               if FBDEQuy.State<>dsEdit then
               begin
                  with FBDEQuy do
                  begin
                     Close;
                     SQL.Clear;
                     SQL.Add(FSQLText);
                     Open;
                     IF Not IsEmpty Then
                        Edit
                     else
                        Func_ShowMessagebox('没有你要修改的记录!',mtInformation,btOK);
                  end;
               end
               else
                  Func_ShowMessagebox('数据集已经是修改状态!',mtInformation,btOK);
            end;
         except
            on E:Exception do
            Func_ShowMessagebox('执行修改操作出错!  错误原因如下:'+Chr(10)+E.message,mtError,btOK);
         end;
      end;end;function ToperateDataSet.JudgeIF: Boolean;
    begin
      if (FADOQuy=nil) and (FBDEQuy=nil) then
      begin
         Func_ShowMessagebox('数据集为空,此操作无效!',mtError,btOK);
         Result:=False;
         Exit;
      end
      else if (FADOQuy<>nil) and (FBDEQuy<>nil)  then
      begin
         Func_ShowMessagebox('数据集太多,此操作无效!',mtError,btOK);
         Result:=False;
         Exit;
      end;  if FSQLText='' then
         FSQLText:=SQL_Text;  if FSQLText='' then
      begin
         Func_ShowMessagebox('没有SQL语句,此操作无效!',mtError,btOK);
         Result:=False;
         Exit;
      end;
      Result:=True;
    end;procedure ToperateDataSet.Modified;
    begin
      ExecuteModified;
    end;end.
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,OperationClass, DB, ADODB, StdCtrls, Grids, DBGrids;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        Button1: TButton;
        Button2: TButton;
        ADOQuery1: TADOQuery;
        DataSource1: TDataSource;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      AddData:ToperateDataSet;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
    AddData.Create(ADOQuery1,nil,'');
    AddData.SQL_Text:='Select * from Sheet where 1=2';
    AddData.Append;
    end;end.当我点Button1时出现错位:Access violation at address 00403AC1 in module 'Project1.exe'. Read of address 00000000
      

  2.   

    AddData :=ToperateDataSet.Create(ADOQuery1,nil,'');
    AddData.SQL_Text:='Select * from Sheet where 1=2';
    AddData.Append;
    试试
    另外
    //constructor Create(ADOQuy:TADOQuery;BDEQuy:TQuery;SQLText:String); 类的Create你得重构一下,就是上面这个方法你得写
      

  3.   

    同意: IwantFlay(我很爱她!!!!!!!!!!) AddData :=ToperateDataSet.Create(ADOQuery1,nil,'');
    try
      AddData.SQL_Text:='Select * from Sheet where 1=2';
      AddData.Append;
    finally
      AddData.Free;
    end;同时
    constructor Create(ADOQuy:TADOQuery;BDEQuy:TQuery;SQLText:String); 
    begin
      inherited;
      FADOQuy:=ADOQuy;
      FBDEQuy:=BDEQuy;
      FSQLText:=SQLText;
    end;看这个类感觉很累:
    1,与问题无关的showxxx占了太多的篇幅(在这里)
    2,应作为私有成员的FADOQuy:TADOQuery;  FBDEQuy:TQuery;  FSQLText:String;却变成了单元内的公用变量,试问,先后创建两个对象,这三个变量该是哪一个对象所需要的内容?
    ----建议将其放在private部分
    3,Function Func_ShowMessagebox为何没有返回值?
    ----不如将其定义为procedure。
    4,function ToperateDataSet.JudgeIF: Boolean;既判断是否XX又显示判断结果,不太符合习惯
    ----将两个功能分开
    5,
               with FADOQuy do
               begin
                  Close;
                  SQL.Clear;
                  SQL.Add(FSQLText);
                  Open; ----------此处是否应为ExecSQL(我不确定)6,ADO,BDE混杂,不妨定义为两个子类。