A simple, adapting to a database access layer for accessing operations between multiple databases

xiaoxiao2021-03-06  44

// Everyone often uses some of the forms of a function that is often used in the form of a database, but if it is a different type of database, or want to convert the database type, it is more troublesome, and it provides three objects .tdataAccess For the base class .TSERVERACCESS, TclientAccess is a BDE type, these are a bit uncomfortable place, that is, there is no initialization entrust function, but in initial, you can improve it yourself, this is just A special case of our application. 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;

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: waiting for assignment} 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; ImplementConstructor TSERVERACCESS.CREATE; begin adoconnection: = tadoconnection.create (nil); end;

DESTRUCTOR TSERVERACCESS.DESTROY; begin adoconnection.free; inherited;

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; beginend ; function TServerAccess.OpenSQL (ASQLStr: string; var ADataSet: TDataSet): Boolean; var tmpADOQuery: TADOQuery; begin tmpADOQuery: = GetCommQuery (); tmpADOQuery.SQL.Text: = ASQLStr; tmpADOQuery.Open; Result: = tmpADOQuery.RecordCount > 0; adataset: = TmpadoQuery; end; constructor tclientAccess.create; begin databaseename: = 'Selfold';

class procedure TClientAccess.CreateAlias; var tmpStrList: TStringList; begin Session.DeleteAlias ​​( 'selfold'); Session.SaveConfigFile; if not Session.IsAlias ​​( 'selfold') then begin tmpStrList: = TStringList.Create; try tmpStrList.Add ( ' Path = ' extractfilepath (application.exename) ' hs_data '); session.addalias (' Selfold ',' Standard ', TMPSTRLIST); session.saveconfigfile; fin or tmpstrlist.free; end; end; end;

procedure TClientAccess.ExecSQL (ASQLStr: string); var tmpQuery: TQuery; begin tmpQuery: = GetCommQuery; tmpQuery.SQL.Text: = ASQLStr; tmpQuery.ExecSQL; FreeCommQuery (tmpQuery); end;

Procedure tclientAccess.freecommquery; begin aquery.free; end;

Function Tclient: tQuery; Begin Result: = tQuery.create (nil); Result.DatabaseName: = DatabaseName; End; Procedure TclientAccess.initall; Begin

END;

function TClientAccess.OpenSQL (ASQLStr: string; var ADataSet: TDataSet): Boolean; var tmpQuery: TQuery; begin tmpQuery: = GetCommQuery; tmpQuery.RequestLive: = True; tmpQuery.SQL.Text: = ASQLStr; tmpQuery.Open; ADataSet: = TMPQUERY;

Result: = TmpQuery.RecordCount> 0;

End; {tdataaccess}

function TDataAccess.GetSQLValue (ASQLStr: string; var AValue: Variant): Boolean; var tmpDataSet: TDataSet; i: Integer; begin Result: = OpenSQL (ASQLStr, tmpDataSet); try if Result then begin AValue: = VarArrayCreate ([0, TMPDataSet.fieldcount], varvariant; for i: = 0 to TMPDataSet.fieldcount - 1 do // ipdatastring; end; // for end; finary; // for end; finally tmpdatanet.close ; TMPDATASET.FREE;

END;

function TDataAccess.GetSQLValue (ASQLStr: string; var AValue: string): Boolean; var tmpDataSet: TDataSet; begin Result: = OpenSQL (ASQLStr, tmpDataSet); try if Result then AValue: = tmpDataSet.Fields [0] .AsString;

Finally TmpDataSet.Close; tmpdatanet.free;

END;

转载请注明原文地址:https://www.9cbs.com/read-75024.html

New Post(0)