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

原创 2006年06月14日 16:34:00

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

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.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JDBC数据库操作通用类

C#操作SQL Server数据库通用类

在前段时间写的文章>一文中,文章链接:[url]http://alligator.blog.51cto.com/36993/102446[/url]。描述了如何处理具有固定格式文本文件的处理方式,并提...

基于C#分步式聊天系统的在线视频直播系统 之 数据库操作基类 - 通用于Windows及Linux的Mono环境

数据库操作基类 项目demo:http://www.liaofuwu.com 环境 支持操作系统:Windows、Linux的Mono 支持数据库:Mysql、Oracle、Sql Server 配置...

数据库通用操作类源码

  • 2015-07-15 15:13
  • 908KB
  • 下载

ADO.NET通用数据库操作类

最近经常看到一些人在秀其多年来积累经验而成的数据库操作类,在此我也秀一把。 本人对ORM相关的东西是极不喜欢的,可能与平常接触到的项目所操作的数据量都较大、业务较复杂吧。另对那种通过自己编写实体类,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)