DELPHI database operations (tools)

DELPHI database connection class

Do when a short head to connect to the database may be made by creating and releasing class, making the server when a request is generated for each data connection via a class

unit UnDm;

interface

uses
  SysUtils, Classes, Controls, Forms, Dialogs,
  ComCtrls, ExtCtrls, Menus, StdCtrls,
  ADODB,
  DB,inifiles;

type
  TRecordResult = (RRError, RRZero, RROK);
  TDataStatus = (DsSel, DsEdit, DsAdd);
type
  TDataConnection = class
    DataAdoConn: TADOConnection;
    qryTemp1: TADOQuery;
    qryTemp2: TADOQuery;
    qryTemp3: TADOQuery;
    qryTemp4: TADOQuery;

  private

  public
    constructor Create; overload;
    //Connect to the database 
    function dbconn: Boolean; 

    // start things 
    function AdoBeginTrans: Boolean;
     // rollback things 
    function AdoRollbackTrans: Boolean;
     // submit things 
    function AdoCommitTrans: Boolean;
     //
     Function GetOpenQuery ( const sSQL, stitle: String; _AdoQry: TADOQuery ; Is_View: Boolean = True): TRecordResult; overload ;
     function updateSQL ( const sSQL, stitle: String; IsView: Boolean = True): TRecordResult;
     function GetAllUser: Integer;
   End ; 


Implementation 
uses Querypa;

constructor TDataConnection.Create;
var
  I:Integer;
begin
  inherited;
  DataAdoConn:=TADOConnection.Create(nil);
  qryTemp1:=TADOQuery.Create(nil);
  qryTemp2:=TADOQuery.Create(nil);
  qryTemp3:=TADOQuery.Create(nil);
  qryTemp4:=TADOQuery.Create(nil);
  DataAdoConn.LoginPrompt:=False;
  qryTemp1.Connection:=DataAdoConn;
  qryTemp2.Connection:=DataAdoConn;
  qryTemp3.Connection:=DataAdoConn;
  qryTemp4.Connection:=DataAdoConn;
end;

function TDataConnection.DbConn: Boolean;
const
  //sDbConn = 'Provider=OraOLEDB.Oracle.1;Password=%s;Persist Security Info=True;User ID=%s;Data Source=%s';
  sDbConn = 'Provider=OraOLEDB.Oracle.1;Password=%s;User ID=%s;Data Source=%s;Persist Security Info=True';
var
  sUdlFile: String;
  ServerIni:TiniFile;
  SOURCE,USER,PASSWORD:string;
  sSql:string;
begin
  //获得数据库连接串
  ServerIni := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'ServerInfor.Ini');
  SOURCE := ServerIni.ReadString('HIS', 'SOURCE', '0');
  USER := ServerIni.ReadString('HIS', 'USER', '0');
  PASSWORD := ServerIni.ReadString('HIS', 'PASSWORD', '0');
  sSql := Format(sDbConn, [PASSWORD, USER, SOURCE]);
  Result := False;
  if DataAdoConn.Connected then
    DataAdoConn.Close;
  //连接数据库
  DataAdoConn.ConnectionString :=sSql;  //'FILE NAME='+sUdlFile;
  Try
    DataAdoConn.Open;
    if not DataAdoConn.Connected then
    begin
      Result :=false;
      Exit;
    end
    else
    begin
      Result :=true;
    end;
  Except
    Result := False;
    Exit;
  end;
end;

function TDataConnection.AdoBeginTrans: Boolean;
Var
  nResult: Integer;
begin
  Result := False;
  Try
    nResult := DataAdoConn.BeginTrans;
    if nResult > 0 then
      Result := True;
  except
    On E: Exception do
    Begin
      SaveError('启动事务', E.Message);
    end;
  end;
end;
function TDataConnection.AdoCommitTrans: Boolean;
Var
  nResult: Integer;
begin
  Result := False;
  Try
    DataAdoConn.CommitTrans;
    Result := True;
  except
    On E: Exception do
    Begin
      SaveError('提交事务', E.Message);
    end;
  end;

end;

function TDataConnection.AdoRollbackTrans: Boolean;
Var 
  the nResult: Integer; 
the begin
  The Result: = False; 

  the Try 
    DataAdoConn.RollbackTrans; 
    the Result: True; 
  the except 
    the On E: Exception do 
    the Begin 
      SaveError ( ' rollback transaction ' , e.Message);
     End ;
   End ;
 End ; 

// / <Summary> 
// / Update Han insert or modify the number of database 
// / sSQL SQL statements 
// / stitle execution title 
// / IsView whether pop-up error message 
// / </ the Summary> 
function TDataConnection.UpdateSql ( const sSQL, stitle: String; 
  IsView: Boolean) : TRecordResult;
Var
  nResult: Integer;
  AdoQry:TADOQuery;
begin
  AdoQry:=TADOQuery.Create(nil);
  AdoQry.Connection:= DataAdoConn;
  Result := RRError;
  With AdoQry do
  Begin
    Close;
    SQL.Text := sSql;
    Try
      nResult := ExecSQL;
      if nResult = 0 then
        Result := RRZero
      else
        Result := RROK;
    The except 
      ON E: Exception do 
      the Begin 
        SaveError (stitle, sSQL + # 10 # 13 is + e.Message); 
        MsgShow (e.Message, . 3 ); 
        the Exit; 
      End ;
     End ;
   End ;
   IF the Result = RRZero the then 
  the Begin 
    SaveError (stitle, sSQL + # 10 # 13 is + ' execution does not return correct results! ' );
     IF IsView the then 
      MsgShow ( ' execution does not return correct results! ' , 0 ); 
    the Exit;
   End ;
 End ; 

// / <the Summary> 
// / statements to query the database 
// / sSQL SQL statements 
// / stitle execution title 
// / _AdoQry ADOQuery controls 
// / IsView whether pop-up error message 
// / </ Summary> 
function TDataConnection.GetOpenQuery ( const sSQL, stitle: String; _AdoQry: TADOQuery; 
  Is_View: Boolean): TRecordResult; 
the begin 
  the Result: = RRError; 
  With _AdoQry do 
  the Begin 
    the Close; 
    SQL.Text: = sSQL; 
    the Try 
      the Open; 
      IF the IsEmpty the then
        Result := RRZero;
      if Not IsEmpty then
        Result := RROK;
    Except
      on E: Exception do
      Begin
        SaveError(sTitle, sSql+ #10#13 +E.Message);
        MsgShow(E.Message, 3);
        Exit;
      end;
    end;
  end;
  if (Result = RRZero) and Is_View then
  Begin
    SaveError(sTitle, sSql+ #10#13 + sTitle);
    MsgShow(sTitle, 3);
  end;
end;


function TDataConnection.GetAllUser: Integer;
begin
  with qryTemp1 do
  begin
    Close;
    SQL.Text := 'SELECT USERID, name From T_USERS';
    Open;
  end;
  Result := qryTemp1.RecordCount;
end;




end.
————————————————

statement

var 
    as a dm: TDataConnection;

create

// preferred database server connection 
  dm: = TDataConnection. The Create ;
   IF  not dm.DbConn the then 
  the begin 
    MsgShow ( ' server database connection failed, unable to start service ' , 1 ); 
    dm.Free; 
    Exit; 
  End ;

Execute SQL statements

  strSQL: = ' the UPDATE the SET A = AA' '. 1' ' ' ; 
  DM.UpdateSql (strSQL, ' update the database ' , to false);

Query the database

  strSQL: = ' the SELECT * the FROM A ' ; 
  DM.GetOpenQuery (strSQL, ' update the database ' , ADOQuery1, to false);

After the transaction would increase the transaction in which you can add TRY

Guess you like

Origin www.cnblogs.com/jijm123/p/11879398.html