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

发布时间:2010年07月13日      浏览次数:490 次
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.
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!