通用的数据库增删改操作类

   数据库程序里的增删改操作实在太多了,我写了个通用类,力图使大量操作都可以用类似下面的代码代替:

procedure TForm1.BtnAddClick(Sender: TObject);
var
  AOpt:TOperation;
  i:integer;
begin
  try
    AOpt:=  TOperation.Create;   //创建操作对象

   
    AOpt.InsertOpt(sSqlInsert,panel1);    //插入panel1上所有数据到数据库

  finally
    AOpt.Free;
  end;

end;

 

下面具体实现:

unit CommonFuncs;

interface
uses  SysUtils,Classes,Variants,ExtCtrls,StdCtrls,Main,DBConn,Messages, RzEdit,QTypes,
      RzCmboBx,Controls,QForms,QDialogs,Forms,Windows;
type
  ControlInfo=record
  OleVar:OleVariant;
  tabOrder:integer;
end;
type
  TCmbBoxInfo=class(TObject)
  Code,Name:string;
end;
Type
  TOperation=class(TObject)
protected
  FArr: array of ControlInfo;
  FArrLength:integer;
  procedure BubbleSort;
  function GetPanelData(APanel:TPanel):integer;
  function GetAnEdtControlData(ACtrlOnPnl:TControl):ControlInfo;
public
  function InfoDataHasExist(sSql:string;AEdtCtrl:TControl;AMsgInfo:string):boolean;
  function InfoTextIsNull(ACtrl:TControl;AMsgInfo:string):boolean;
  function InitCmbBox(sSql:string;ACmbBox:TComboBox): boolean;
  function InitRzCmbBox(sSql:string;ARzCmbBox:TRzComboBox): boolean;
  function InsertOpt(AInsertSql:string;const APanel:TPanel):boolean;
  function ModifyOpt(AnUpdateSql:string;ID:OleVariant;const APanel:TPanel):boolean;
end;

implementation
function TOperation.InfoTextIsNull(ACtrl:TControl;AMsgInfo:string):boolean;
var
  AControlInfo:ControlInfo;
begin
  result:=false;
  AControlInfo:=GetAnEdtControlData(ACtrl);
  if  AControlInfo.tabOrder<>-2 then
  begin
    if AControlInfo.OleVar='' then
    begin
      result:=true;
      ShowMessage(AMsgInfo);
      (ACtrl as TWinControl).SetFocus;
    end;
  end;
end;
function TOperation.InfoDataHasExist(sSql:string;AEdtCtrl:TControl;AMsgInfo:string):boolean;
begin
  if CenterDB=nil then CenterDB:=TCenterDb.Create(nil);
  if not CenterDB.IsConnected  then  CenterDB.ConnectDB;
  CenterDB.QueryDB(sSql,GetAnEdtControlData(AEdtCtrl).OleVar);
  if CenterDB.ReordCount=0 then
    result:=false
  else
    begin
      result:=true;
      Application.MessageBox(PCHAR(AMsgInfo), 'Hint', MB_OK);
    end;
end;

function TOperation.GetAnEdtControlData(ACtrlOnPnl:TControl):ControlInfo;
begin
    if ACtrlOnPnl.ClassType=TEdit then
    begin
      result.OleVar:=Trim(TEdit(ACtrlOnPnl).Text);
      result.TabOrder:=TEdit(ACtrlOnPnl).TabOrder;
      exit;
    end;
    if ACtrlOnPnl.ClassType=TRzEdit then
    begin
      result.OleVar:=Trim(TRzEdit(ACtrlOnPnl).Text);
      result.TabOrder:=TRzEdit(ACtrlOnPnl).TabOrder;
      exit;
    end;
    if ACtrlOnPnl.ClassType=TMemo then
    begin
      result.OleVar:=Trim(TMemo(ACtrlOnPnl).Text);
      result.TabOrder:=TMemo(ACtrlOnPnl).TabOrder;
       exit;
    end;
    if ACtrlOnPnl.ClassType=TRzDateTimeEdit then
    begin
      result.OleVar:=FormatDateTime('yyyy-mm-dd',TRzDateTimeEdit(ACtrlOnPnl).Date);
      result.TabOrder:=TRzDateTimeEdit(ACtrlOnPnl).TabOrder;
       exit;
    end;
    if ACtrlOnPnl.ClassType=TRzNumericEdit then
    begin
      result.OleVar:=TRzNumericEdit(ACtrlOnPnl).IntValue;
      result.TabOrder:=TRzNumericEdit(ACtrlOnPnl).TabOrder;
       exit;
    end;
    if ACtrlOnPnl.ClassType=TCombobox then
    begin
      result.OleVar:='';
      if TCombobox(ACtrlOnPnl).ItemIndex<>-1 then
        result.OleVar:=TCmbBoxInfo(TCombobox(ACtrlOnPnl).Items.Objects[TCombobox(ACtrlOnPnl).ItemIndex]).Code;
      result.TabOrder:=TCombobox(ACtrlOnPnl).TabOrder;
       exit;
    end;
    if ACtrlOnPnl.ClassType=TRzCombobox then
    begin
      result.OleVar:='';
      if TRzCombobox(ACtrlOnPnl).ItemIndex<>-1 then
        result.OleVar:=TCmbBoxInfo(TRzCombobox(ACtrlOnPnl).Items.Objects[TRzCombobox(ACtrlOnPnl).ItemIndex]).Code;
      result.TabOrder:=TRzCombobox(ACtrlOnPnl).TabOrder;
      exit;
    end;
    result.tabOrder:=-2;
end;
function TOperation.InitCmbBox(sSql:string;ACmbBox:TComboBox): boolean;
var
  CmbBoxInfo:TCmbBoxInfo;
begin
   result:=false;
  if CenterDB=nil then CenterDB:=TCenterDB.Create(nil);
  if not CenterDb.IsConnected   then CenterDB.ConnectDB;
  CenterDB.QueryDB(sSql);
  ACmbBox.Items.Clear;
  while not CenterDB.EndOfQuery do
   begin
     CmbBoxInfo:=TCmbBoxInfo.Create;
     CmbBoxInfo.Code:=CenterDB.FieldAsString('DataCode');
     CmbBoxInfo.Name:=CenterDB.FieldAsString('DataValue');
     ACmbBox.Items.AddObject(CmbBoxInfo.Name,CmbBoxInfo);
     CenterDB.NextRecord;
   end;
   CenterDB.CloseQuery;
   CenterDB.Disconnect;
   result:=true;
end;

function TOperation.InitRzCmbBox(sSql:string;ARzCmbBox:TRzComboBox): boolean;
var
  CmbBoxInfo:TCmbBoxInfo;
begin
   result:=false;
  if CenterDB=nil then CenterDB:=TCenterDB.Create(nil);
  if not CenterDb.IsConnected   then CenterDB.ConnectDB;
  CenterDB.QueryDB(sSql);
  ARzCmbBox.Items.Clear;
  while not CenterDB.EndOfQuery do
   begin
     CmbBoxInfo:=TCmbBoxInfo.Create;
     CmbBoxInfo.Code:=CenterDB.FieldAsString('DataCode');
     CmbBoxInfo.Name:=CenterDB.FieldAsString('DataValue');
     ARzCmbBox.Items.AddObject(CmbBoxInfo.Name,CmbBoxInfo);
     CenterDB.NextRecord;
   end;
   CenterDB.CloseQuery;
   CenterDB.Disconnect;
   result:=true;
end;
function TOperation.GetPanelData(APanel:TPanel):integer;
var
  i,EdtControlCount:integer;
  AControlInfo:ControlInfo;
begin
  EdtControlCount:=0;
  for i:=0 to  APanel.ControlCount-1 do
  begin
    AControlInfo:=GetAnEdtControlData(APanel.controls[i]);
    if   AControlInfo.tabOrder<>-2   then
    begin
      FArr[EdtControlCount].OleVar:=AControlInfo.OleVar;
      FArr[EdtControlCount].TabOrder:=AControlInfo.tabOrder;
      Inc(EdtControlCount);
    end;
  end;
  result:=EdtControlCount;
end;
procedure TOperation.BubbleSort;
var
  i,j:integer;
  Temp:ControlInfo;
begin
  if FArrLength<2 then exit;
  i:=0;
  while i<FArrLength-1 do
  begin
    j:=FArrLength-1;
    while(j>i) do
    begin
      if   FArr[j].tabOrder<FArr[j-1].tabOrder then
      begin
        Temp:=FArr[j-1];
        FArr[j-1]:=FArr[j];
        FArr[j]:=Temp;
      end;
      j:=j-1;
    end;
    Inc(i);
  end;
end;
function TOperation.InsertOpt(AInsertSql:string;const APanel:TPanel):boolean;
var
  i:integer;
  sSQL:string;
  Arr:array of OleVariant;
begin
  result:=false;
  SetLength(FArr,APanel.ControlCount);
  FArrLength:= GetPanelData(APanel);
  BubbleSort;
  SetLength(Arr,FArrLength);
  for i:=0 to  FArrLength-1 do
  Arr[i]:= FArr[i].OleVar;
  try
    if not Centerdb.IsConnected then
      Centerdb.ConnectDB;
    CenterDB.ExecSql(AInsertSql,arr);
  finally
    Centerdb.Disconnect;
  end;
  result:=true;
end;

function TOperation.ModifyOpt(AnUpdateSql:string;ID:OleVariant;const APanel:TPanel):boolean;
var
  i:integer;
  sSQL:string;
  Arr:array of OleVariant;
begin
  result:=false;
  SetLength(FArr,APanel.ControlCount);
  FArrLength:= GetPanelData(APanel);
  BubbleSort;
  SetLength(Arr,FArrLength+1);       //这里的arr比插入操作的时候多一个值'sID'
  for i:=0 to  FArrLength-1 do
    Arr[i]:= FArr[i].OleVar;
  Arr[FArrLength]:=ID;
  try
    if not Centerdb.IsConnected then
      Centerdb.ConnectDB;
    CenterDB.ExecSql(AnUpdateSql,arr);
  finally
    Centerdb.Disconnect;
  end;
  result:=true;

end;
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值