//大家在进行数据库访问的时候,经常会将自己常用的一些写成函数的形式, 但如果是不同类型的数据库,或者想转换数据库类型,就比较麻烦了,此处提供了三个对象.TDataAccess为基类.TServerAccess为ADO类型的,TClientAccess为BDE类型的, 这几个都有点不太舒服的地方,就是没有提供初始化委托函数,而是用一个INITALL来进行初始化,大家可以自己改进,这只是我们应用的一个特例.
unit DataAccess_U; interfaceuses Classes, ADODB, DB, SysUtils, Dbtables, Variants, Forms; type TDataAccess = class public function GetSQLValue(ASQLStr: string; var AValue: Variant): Boolean; overload; virtual; function GetSQLValue(ASQLStr: string; var AValue: string): Boolean; overload; virtual; procedure ExecSQL(ASQLStr: string); virtual; abstract; function OpenSQL(ASQLStr: string; var ADataSet: TDataSet): Boolean; virtual; abstract; end; TServerAccess = class(TDataAccess) private function GetCommQuery: TADOQuery; procedure FreeCommQuery(AQuery: TADOQuery); public ADOConnection: TADOConnection; ConnectString: string; constructor Create; destructor Destroy; override; procedure ExecSQL(ASQLStr: string); override; function OpenSQL(ASQLStr: string; var ADataSet: TDataSet): Boolean; override; procedure InitAll; virtual; end; TClientAccess = class(TDataAccess) private DataBaseName: string; //{ DONE : 等待赋值 } function GetCommQuery: TQuery; procedure FreeCommQuery(AQuery: TQuery); public constructor Create; procedure ExecSQL(ASQLStr: string); override; function OpenSQL(ASQLStr: string; var ADataSet: TDataSet): Boolean; override; class procedure CreateAlias; procedure InitAll; end;implementationconstructor TServerAccess.Create;begin ADOConnection := TADOConnection.Create(nil);end; destructor TServerAccess.Destroy;begin ADOConnection.Free; inherited;end; procedure TServerAccess.ExecSQL(ASQLStr: string);var tmpQuery: TADOQuery;begin tmpQuery := GetCommQuery; try tmpQuery.Close; tmpQuery.SQL.Text := ASQLStr; tmpQuery.ExecSQL; finally freeCommQuery(tmpQuery); end;end;procedure TServerAccess.FreeCommQuery(AQuery: TADOQuery);begin FreeAndNil(AQuery);end; function TServerAccess.GetCommQuery: TADOQuery;begin Result := TADOQuery.Create(nil); Result.Connection := ADOConnection;end;procedure TServerAccess.InitAll;begin end;function TServerAccess.OpenSQL(ASQLStr: string