⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cls_baseclass.pas

📁 用Delhpi和mapx开发的警务管理系统
💻 PAS
字号:
unit cls_BaseClass;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,winsock,inifiles
  ,ADOdb, StdCtrls, Db,AppEvnts;
  
type 
  TINI=class     //INI操作类
  public
    function sReadINI(sSegment:string;sItem:string;sDefaultValue:string):string;
    function sWriteINI(sSegment:string;sItem:string;sValue:string):integer;
  end;

  TDataOperate=class  //数据库操作类
  private
    m_dbConnecct:TADOConnection;
    m_DataBaseName:string;
  public
    msProvider:string;
    msUserID:string;
    msPassword:string;
    msDataSource:string;
    msHostName:string;
    msConnectString:string;
    mAdoCommonQry:TADOQuery;    //查询数据集
    constructor create(DbConnect:TADOConnection;sDataBaseName:string);
    function blnConnectDB():boolean;
    function blnReConnectDB():boolean;
    function blnExecuteSQL(sSQL:string):boolean;
    function adoGetAdoQuery(sSQL:string):TADOQuery;
    function sGetProvider:string;
    function sGetUserID:string;
    function sGetPassword:string;
    function sGetDataSource:string;
    function sGetConnectString:string;
    function sGetHostName:string;
    //function adoGetAdoQuery(sSQL:string;blnStatic:boolean):TADOQuery;overload;
  end;

  TPublic=class   //公用函数类
    private
      msUserName:string;                 // 登录用户名
      msUserDept:string;                 // 登录用户部门
      mlBackgroundColor:longint;          // 系统背景颜色
      mlFontColor:longint;                 // 系统前景颜色
      msSystemUnit:string;                // 当前系统的使用单位
      mblnWriteLog:boolean;               // 是否写系统日志
      msComputerName:string;             // 当前计算机名
      msIPAddress:string;               //当前计算机IP地址
      //function sGetMaxActID: Integer;
      //function bSetRoleStr(RoleID: Integer): Boolean;
      //function bSetRoleStrByMod(ModID: Integer): Boolean;
    public
      msRoleStr: String;// 权限控制字
      //constructor create(sDataBaseName:string);
      //function bSetRole: Boolean;
      function iInitClass:smallint; 
      function lGetBackgroundColor:Longint;
      function lGetFontColor:Longint;
      function sGetCompName:string;
      function sGetIPAddress:string;
      function sGetUserName:string;
      function sGetUserDept:string;
      function iSetUserName(sUserName:string):smallint;
      function iSetUserDept(sUserDept:string):smallint;
  end;
implementation
uses
  frmSystemDM;
{ TINI }
                 //读取INI文件函数
function TINI.sReadINI(sSegment:string;sItem:string;sDefaultValue:string):string; //segmeng:节    sItem:项    sDefault:默认值
var
  tempINI:TIniFile;
  STemp:String;
begin
  tempINI:=nil;
  STemp:=Application.ExeName;
  sTemp:=ExtractFileName(Application.ExeName);
  if pos('.',sTemp)>0 then
    sTemp:=copy(sTemp,1,pos('.',sTemp))+'ini';
  sTemp:=ExtractFilePath(Application.ExeName)+sTemp;
  try
    tempINI:=TIniFile.Create(STemp);
    result:=tempINI.ReadString(sSegment,sItem,sDefaultValue); //如果读取的节不存在返回sDefaultValue的值
  finally
    tempini.Free;
  end;
end;
                 //写INI文件函数     写入成功返回1   写入失败返回-1
function TINI.sWriteINI(sSegment:string;sItem:string;sValue:string):integer;
var
  tempINI:TIniFile;
  STemp:String;
begin
  tempINI:=nil;
  sTemp:=ExtractFileName(Application.ExeName);
  if pos('.',sTemp)>0 then
    sTemp:=copy(sTemp,1,pos('.',sTemp))+'ini';
  sTemp:=ExtractFilePath(Application.ExeName)+sTemp;
  try
    try
      result:=0;
      tempINI:=TIniFile.Create(STemp);
      tempINI.WriteString(sSegment,SItem,sValue);
      result:=1;                        //写入成功返回1
    finally
      tempINI.Free;
    end;
  except
    result:=-1;                       //写入失败
  end;
end; 

{ TDataOperate}
               //执行SQL语句,返回数据集
constructor TDataOperate.create(DbConnect:TADOConnection;sDataBaseName:string);
begin
  m_dbConnecct:=DbConnect;
  m_DataBaseName:= sDataBaseName;
  msProvider:=sGetProvider;
  msUserID:=sGetUserID;
  msPassword:=sGetPassword;
  msDataSource:=sGetDataSource;
  msConnectString:=sGetConnectString;
  msHostName:=sGetHostName;
end;
function TDataOperate.adoGetAdoQuery(sSQL: string): TADOQuery;
var
  adoQue:TADOQuery;
  ConnectString:string;
  cINIFile:TINI;
  tempPath:string;
begin
  cINIFile:=TINI.Create;
  tempPath:=ExtractFilePath(Application.ExeName);
  adoQue:=TADOQuery.Create(nil);
    try
      ConnectString:='Provider=' +cINIFile.sReadINI(m_DataBaseName,'Provider','') + ';' +'Data Source='+ tempPath + cINIFile.sReadINI(m_DataBaseName,'Data Source','');
      if m_dbConnecct.connected=false then         //如果数据库没有打开
        begin
          m_dbConnecct.ConnectionString:=ConnectString;
          m_dbConnecct.LoginPrompt:=false;
          m_dbConnecct.open;
        end;
      with adoQue do
        begin
          Connection:=m_dbConnecct;
          close;
          SQL.Clear;
          SQL.Add(sSQL);
          open;                //执行SQL语句
        end;
      Result := adoQue;
      cINIFile.Free;
    except
      on E:exception do
        begin
          cINIFile.Free;
          ShowMessage(E.Message);
        Result:=nil;
        end;
    end;
end;
               //连接数据库   连接成功返回True   连接失败返回False
function TDataOperate.blnConnectDB(): boolean;
var
  ConnectString:string;
  cINIFile:TINI;
  tempPath:string;
begin
  cINIFile:=TINI.Create;
  tempPath:=ExtractFilePath(Application.ExeName);
  with m_dbConnecct do
    try
      ConnectString:='Provider=' +cINIFile.sReadINI(m_DataBaseName,'Provider','') + ';' +'Data Source='+ tempPath + cINIFile.sReadINI(m_DataBaseName,'Data Source','');
      LoginPrompt:=false;
      if connected=false then
        begin
          ConnectionString:=ConnectString;
          open;
          cINIFile.Free;
        end;
      Result:=true;
    except
      on E:exception do
        begin
          Result:=false;
          cINIFile.Free;
          showmessage(E.Message);
        end;
    end;

end;
               //执行SQL语句   执行成功返回True  失败返回False
function TDataOperate.blnExecuteSQL(sSQL: string): boolean;
var
  adoQue:TADOQuery;
  ConnectString:string;
  cINIFile:TINI;
  tempPath:string;
begin
  cINIFile:=TINI.Create;
  tempPath:=ExtractFilePath(Application.ExeName);
  adoQue:=TADOQuery.Create(nil);
  try
    ConnectString:='Provider=' +cINIFile.sReadINI(m_DataBaseName,'Provider','') + ';' +'Data Source='+ tempPath + cINIFile.sReadINI(m_DataBaseName,'Data Source','');
    if m_dbConnecct.connected=false then         //如果数据库没有打开
        begin
          m_dbConnecct.ConnectionString:=ConnectString;
          m_dbConnecct.LoginPrompt:=false;
          m_dbConnecct.open;
        end;
      with adoque do
        begin
          Connection:=m_dbConnecct;
          SQL.Clear;
          SQL.Add(sSQL);
          if Prepared=false then
             prepared;
             ExecSQL;                //执行SQL语句
        end;
        Result:=true;
        adoQue.free;
      except
        on E:exception do
          begin
            Result:=false;
            adoQue.free;
            ShowMessage(E.Message);
          end;
      end;
end;
                  //重新连接数据库
function TDataOperate.blnReConnectDB(): Boolean;
var
  ConnectString:string;
  cINIFile:TINI;
  tempPath:string;
begin
  cINIFile:=TINI.Create;
  tempPath:=ExtractFilePath(Application.ExeName);
  with m_dbConnecct do
    try
      ConnectString:='Provider=' +cINIFile.sReadINI(m_DataBaseName,'Provider','') + ';' +'Data Source='+ tempPath + cINIFile.sReadINI(m_DataBaseName,'Data Source','');
      if connected=false then
        begin
          LoginPrompt:=false;
          ConnectionString:=ConnectString;
          open;
        end;
        cINIFile.Free;
        cINIFile.Free;
        result:=true;
    except
      result:=false;
      cINIFile.Free;
    end;
end;

//******返回连接字符串的Provider*****//
//******读取失败返回空字符***********//
function TDataOperate.sGetProvider:string;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    result:=cINIFile.sReadINI(m_DataBaseName ,'Provider','');
    cINIFile.Free;
  except
    result:='';
    cINIFile.Free;
  end;
end;

//******返回连接字符串的UserID*****//
//******读取失败返回空字符***********//
function TDataOperate.sGetUserID:string;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    result:=cINIFile.sReadINI(m_DataBaseName,'User ID','');
    cINIFile.Free;
  except
    result:='';
    cINIFile.Free;
  end;
end;
//******返回连接字符串的Password*****//
//******读取失败返回空字符***********//
function TDataOperate.sGetPassword:string;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    result:=cINIFile.sReadINI(m_DataBaseName,'Password','');
    cINIFile.Free;
  except
    result:='';
    cINIFile.Free;
  end;
end;
//******返回连接字符串的data source*****//
//******读取失败返回空字符***********//
function TDataOperate.sGetDataSource:string;
var
  cINIFile:TINI;
  tempPath:string;
begin
  tempPath:=ExtractFilePath(Application.ExeName);
  cINIFile:=TINI.Create;
  try
    result:= tempPath + cINIFile.sReadINI(m_DataBaseName,'Data Source','');
    cINIFile.Free;
  except
    result:='';
    cINIFile.Free;
  end;
end;
//******返回连接字符串******//
function TDataOperate.sGetConnectString:string;
var
  cINIFile:TINI;
  tempPath:string;
begin
  tempPath:=ExtractFilePath(Application.ExeName);
  cINIFile:=TINI.Create;
  try
    result:='Provider=' +cINIFile.sReadINI(m_DataBaseName,'Provider','') + ';' +'Data Source='+ tempPath + cINIFile.sReadINI(m_DataBaseName,'Data Source','');
    cINIFile.Free;
  except
    result:='';
    cINIFile.Free;
  end;
end;
//******获取服务器名******//
function TDataOperate.sGetHostName:string;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    result:=cINIFile.sReadINI(m_DataBaseName,'Host Name','');
    cINIFile.Free;
  except
    result:='';
    cINIFile.Free;
  end;
end;

{ TPublic }
          //初始化   计算机名,IP地址,前景色,背景色,是否写日志,使用单位
function Tpublic.iInitClass:smallint;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    msComputerName:=sGetCompName;
    msIPAddress:=sGetIPAddress;
    mlBackgroundColor:=StrToInt64(cINIFile.sReadINI('SYSTEM','BACKGROUNDCOLOR','1827349'));
    mlFontColor:=StrToInt64(cINIFile.sReadINI('SYSTEM','FONTCOLOR','0'));
    if UpperCase(cINIFile.sReadINI('LOG','WRITELOG','True'))='TRUE' then
      mblnWriteLog:=True
    else
      mblnWriteLog:=False;
    result:=1;
    cINIFile.Free;
  except
    result:=0;
    cINIFile.Free;
  end;
end;

function Tpublic.lGetBackgroundColor:Longint;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    result:=strtoint(cINIFile.sReadINI('SYSTEM','BACKGROUNDCOLOR','1827349'));
  except
    result:=1827349;
  end;
  cINIFile.Free;
end;
        //获得背景色
function Tpublic.lGetFontColor:Longint;
var
  cINIFile:TINI;
begin
  cINIFile:=TINI.Create;
  try
    result:=strtoint(cINIFile.sReadINI('SYSTEM','FONTCOLOR','0'));
  except
    result:=0;
  end;
  cINIFile.Free;
end;
function Tpublic.sGetCompName:string;         //返回计算机名
var
  temp_str : String;
  buf  : array [0..255] of Char;
  name_len :DWord;
begin
  result := '';
  name_len:=255;
  if GetComputerName(@buf,name_len)=true then
    temp_str := buf
  else
    temp_Str := '<Error>' ;
  result := temp_Str ;
end;
      //返回IP地址
function Tpublic.sGetIPAddress:string;
type
  TaPInAddr = array [0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe  : PHostEnt;
  pptr : PaPInAddr;
  Buffer : array [0..63] of char;
  I    : Integer;
  GInitData      : TWSADATA;
begin
  WSAStartup($101, GInitData);
  Result := '';
  GetHostName(Buffer, SizeOf(Buffer));
  phe :=GetHostByName(buffer);
  if phe = nil then Exit;
  pptr := PaPInAddr(Phe^.h_addr_list);
  I := 0;
  while pptr^[I] <> nil do begin
    result:=StrPas(inet_ntoa(pptr^[I]^));
    Inc(I);
  end;
  WSACleanup;
end;
      //获得用户名
function Tpublic.sGetUserName:string;
begin
  result:=msUserName;
end;
       //获得用户单位
function Tpublic.sGetUserDept:string;
begin
  Result:=msUserDept;
end;
                  //赋值用户名
function Tpublic.iSetUserName(sUserName:string):smallint;
begin
  try
    msUserName:=sUserName;
    Result:=1;
  except
    result:=0;
  end;
end;
                //赋值用户单位
function Tpublic.iSetUserDept(sUserDept:string):smallint;
begin
  try
    msUserDept:=sUserDept;
    Result:=1;
  except
    result:=0;
  end;
end;   

//


{function TPublic.sGetMaxActID: Integer;
var
 Qry_Temp: TADOQuery;
 MaxID: Integer;
begin
 Qry_Temp := SystemDM.Glb_DataOperate.adoGetAdoQuery('Select Max(ActionID) as ActID from bm_Sys_ModuleDef');
 MaxID := Qry_Temp.FieldByName('ActID').AsInteger;
 Qry_Temp.Free;
 Result := MaxID;
end;}

{function TPublic.bSetRole: Boolean;
var
 MaxAct,I: Integer;
 Qry_Temp: TADOQuery;
begin
 msRoleStr := '';
 MaxAct := sGetMaxActID;
 for I := 1 to MaxAct do
  msRoleStr := msRoleStr + '0';
 Qry_Temp := SystemDM.Glb_DataOperate.adoGetAdoQuery(Format('Select RoleID from bm_Info_StaffRole where StaffID = ''%s''',[msUserName]));
 for I := 0 to Qry_Temp.RecordCount - 1 do
   begin
     bSetRoleStr(Qry_Temp.FieldByName('RoleId').AsInteger);
   end;
 Qry_Temp.Free;
 Result := True;
end;}

{function TPublic.bSetRoleStr(RoleID: Integer): Boolean;
var
 I: Integer;
begin
  with TADOQuery.Create(nil)do
  try
    Connection := SystemDM.dbMain;
    Sql.Text := Format('Select ModuleID From bm_Info_RoleModule where RoleID = %d',[RoleID]);
    Open;
    for I := 0 to RecordCount - 1 do
     begin
      bSetRoleStrByMod(FieldByName('ModuleID').AsInteger );
     end;
    Result := True;
  finally
    Free;
  end;
end;}

{function TPublic.bSetRoleStrByMod(ModID: Integer): Boolean;
var
 I: Integer;
begin
 with TADOQuery.Create(nil)do
 try
  Connection := SystemDM.dbMain;
  Sql.Text := Format('Select ActionID From bm_Sys_ModuleDef where ModuleID = %d',[ModID]);
  Open;
  for I := 0 to RecordCount - 1 do
   begin
     msRoleStr[FieldByName('ActionID').AsInteger] := '1';
   end;
  Result := True;
 finally
  Free;
 end;
end;}

end.



  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -