RtTool/Delphi7/开发档案/马国钢开发代码/ThreeFun/ComFunc/U_ADOFunc.pas

389 lines
11 KiB
ObjectPascal
Raw Permalink Normal View History

2025-01-08 11:55:07 +08:00
unit U_ADOFunc;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, StdCtrls,DBGrids, DB, cxDBData, ADODB,StrUtils,
Midas, ExtCtrls, Buttons,DBClient,FTComboBox,BtnEdit;
function ISNotOnly(ADOQry:TADOQuery;fieldname: string;tablename: string; str_vari:string): boolean;
function WriteLog(ADOQry:TADOQuery;mModel:string;mAction:string;OpEvent:string):Boolean;
procedure GetERPSetQryTimeValue(ADOQry:TADOQuery);
function DelData(ADOQueryCmd:TADOQuery;mDelStr:String;mInt:Integer):Boolean;
procedure Setsavedata(ADOQueryCmd:TADOQuery;MyTable:string;
Myparent:TWinControl;MyTag:integer);
function GetFieldValue(sqlStr:String;mcaption:string;ADOQuerytmp:TADOQuery):Boolean;
procedure CreateCDSChg(SADOQry:TADOQuery; mClientDataset:TclientDataSet; AColCount: Integer);
function ADORowChgToCol(fromADoQry:TADOQuery;toClientSet:TclientDataSet;
colCount:integer;GroupFields:string;chnField:string):Boolean;
procedure GetYearPeriod(AdoQueryTemp:TADOQuery;FDate:TDateTime);
function ADOColToRow(fromADoQry:TADOQuery;toClientSet:TclientDataSet;
chgFields:string;chgCaptions:string;
TocolCount:integer):boolean;
implementation
uses
U_global,U_formPas,U_commFunc;
////<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
function ISNotOnly(ADOQry:TADOQuery;fieldname: string;tablename: string; str_vari:string): boolean;
begin
result:=true;
try
with ADOQry do
begin
close;
sql.Clear;
sql.Add('select '+fieldname+' from '+tablename);
sql.Add('where '+fieldname+'='''+str_vari+'''' );
open;
if (recordcount>0) and (not fields[0].isnull) then
result:=false;
close;
end;
except
end;
end;
//////////////////////////////////////////////////////
function WriteLog(ADOQry:TADOQuery;mModel:string;mAction:string;OpEvent:string):Boolean;
begin
///////////////////////////////////
//д<><D0B4>־
try
with ADOQry do
begin
close;
sql.Clear ;
sql.Add('select * from xs_sysLog');
sql.Add('where 1<>1');
Open;
Append;
fieldByName('Operor').Value :=gUserName;
fieldByName('OperTime').Value :=gserverDate;
fieldByName('Model').Value :=mModel;
fieldByName('acction').Value :=mAction;
fieldByName('result').Value :='<27>ɹ<EFBFBD>';
fieldByName('OpEvent').Value :=trim(OpEvent);
Post;
end;
result:=true;
except
result:=false;
end;
end;
///////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////
procedure GetERPSetQryTimeValue(ADOQry:TADOQuery);
begin
try
with ADOQry do
begin
close;
sql.Clear ;
sql.Add('select * from XS_SysParam');
Open;
if RecordCount>0 then
begin
gQryBegTime:=fieldByName('QryBegTime').AsString ;
gQryEndTime:=fieldByName('QryEndTime').AsString ;
end
else
begin
gQryBegTime:='00:00';
gQryEndTime:='00:00';
end;
end;
except
end;
end;
//////////////////////////////////////////////
//////<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>SQL<51><4C><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//////////////////////////////////////////////
function DelData(ADOQueryCmd:TADOQuery;mDelStr:String;mInt:Integer):Boolean;
begin
try
result:=False;
with ADOQueryCmd do
begin
Close;
SQL.Clear;
SQL.Add(mDelStr);
ExecSQL;
end;
result:=True;
except
result:=False;
Application.MessageBox('<27><><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>ʧ<EFBFBD>ܣ<EFBFBD>','<27><>ʾ',0);
end;
end;
/////////////////////////////////////////////////////
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ֶθ<D6B6>ֵ
/////////////////////////////////////////////////////
procedure Setsavedata(ADOQueryCmd:TADOQuery;MyTable:string;
Myparent:TWinControl;MyTag:integer);
var
i:Integer;
begin
with Myparent do
begin
for i:=0 to ControlCount-1 do
begin
if Controls[i].Tag=MyTag then
begin
if Controls[i] is TEdit then
begin
ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TEdit(Controls[i]).Text);
end else
if Controls[i] is TRichEdit then
begin
ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TRichEdit(Controls[i]).Text;
end else
if Controls[i] is TComboBox then
begin
ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TComboBox(Controls[i]).Text);
end else
if Controls[i] is TDateTimePicker then
begin
if TDateTimePicker(Controls[i]).ShowCheckbox then
begin
if TDateTimePicker(Controls[i]).Checked then
ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
end else
ADOQueryCmd.FieldByName(Controls[i].Name).Value:=TDateTimePicker(Controls[i]).DateTime;
end else
if Controls[i] is TBtnEditA then
begin
ADOQueryCmd.FieldByName(Controls[i].Name).Value:=Trim(TBtnEditA(Controls[i]).TxtCode);
end;
end;
end;
end;
end;
////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////
function GetFieldValue(sqlStr:String;mcaption:string;ADOQuerytmp:TADOQuery):Boolean;
begin
result:=false;
try
with ADOQuerytmp do
begin
close ;
sql.Clear ;
sql.Add(sqlStr);
Open;
end;
result:=true;
except
application.MessageBox(pchar('<27><>ȡ<EFBFBD><C8A1>'+mcaption+'<27><><EFBFBD>ֶ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>!'),'<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ',0);
end;
end;
//////////////////////////////////////////////////////////////////
procedure GetYearPeriod(AdoQueryTemp:TADOQuery;FDate:TDateTime);
var
fsj:string;
begin
fsj:=FormatDateTime('yyyy-MM-dd',FDate);
with AdoQueryTemp do
begin
Close;
SQL.Clear;
SQL.Add('select * from gy_kjrlb where Qsrq<='''+fsj+''' ');
SQL.Add(' and Zzrq>='''+fsj+'''');
Open;
end;
with AdoQueryTemp do
begin
if IsEmpty then
begin
Application.MessageBox('<27><>δ<EFBFBD><CEB4><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD>ڵĻ<DAB5><C4BB><EFBFBD><EFBFBD>ڼ䣡','<27><>ʾ',0);
Exit;
end;
gkjyear:=fieldbyname('kjYear').AsInteger;
gperiod:=fieldbyname('period').AsInteger;
end;
end;
///////////////////////////////////////////////////////////
//mClientDataset:TclientDataSet
///////////////////////////////////////////////////////////
function ADORowChgToCol(fromADoQry:TADOQuery;toClientSet:TclientDataSet;
colCount:integer;GroupFields:string;chnField:string):Boolean;
var
i:integer;
k:integer;
idx:integer;
begin
if colCount<=0 then exit;
if GroupFields='' then exit;
if chnField='' then exit;
CreateCDSChg(fromADoQry, toClientSet, colCount);
with fromADoQry do
begin
first;
while not eof do
begin
toClientSet.Last;
if Trim(toClientSet.FieldByName(GroupFields).AsString) = Trim(fieldByName(GroupFields).AsString) then
// if toClientSet.locate(GroupFields,VarArrayOf([trim(fieldByName(GroupFields).AsString)]),[]) then
begin
idx:=toClientSet.fieldByName('curIndex').asInteger;
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (idx >= 1) and (idx < colCount) then
begin
toClientSet.Edit;
toClientSet.fieldByName('chgField'+intTostr(idx + 1)).value:=fieldByName(chnField).asfloat;
toClientSet.fieldByName('curIndex').value:=toClientSet.fieldByName('curIndex').AsInteger+1;
toClientSet.Post;
end
//<2F><><EFBFBD>л<EFBFBD><D0BB><EFBFBD>
else
begin
toClientSet.append;
for k:=0 to fields.count-1 do
begin
toClientSet.fields[k].value:= fields[k].value;
end;
for i:=1 to colcount do
begin
toClientSet.FieldByName('chgField'+intToStr(i)).Value := 0;
end;
toClientSet.fieldByName('chgField1').value:=fieldByName(chnField).asfloat;
toClientSet.fieldByName('curIndex').value:= 1;
toClientSet.Post;
end;
end
else
begin
toClientSet.append;
for k:=0 to fields.count-1 do
begin
toClientSet.fields[k].value:= fields[k].value;
end;
for i:=1 to colcount do
begin
toClientSet.FieldByName('chgField'+intToStr(i)).Value := 0;
end;
toClientSet.fieldByName('chgField1').value:=fieldByName(chnField).asfloat;
toClientSet.fieldByName('curIndex').value:= 1;
toClientSet.Post;
end;
next;
end;
end;
end;
procedure CreateCDSChg(SADOQry:TADOQuery; mClientDataset:TclientDataSet; AColCount: Integer);
var
i: Integer;
mFieldName: String;
mSize: Integer;
begin
mFieldName := '';
mClientDataset.Close;
mClientDataset.FieldDefs.Clear;
with SADOQry do
begin
for i := 0 to FieldCount - 1 do //
begin
if (Fields[i].DataType = ftString) and (Fields[i].Size = 0) then
begin
mSize := 1;
end
else
begin
mSize := Fields[i].Size;
end;
mFieldName := Trim(Fields[i].FieldName);
mClientDataset.FieldDefs.Add(mFieldName, Fields[i].DataType, mSize);
end;
end;
for i := 1 to AColCount do
begin
mClientDataset.FieldDefs.Add('chgField' + IntToStr(i), ftFloat, 0);
end;
mClientDataset.FieldDefs.Add('curIndex', ftInteger, 0);
mClientDataset.CreateDataSet;
end;
/////////////////////////////////////////////////////
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD>תΪ<D7AA><CEAA>
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC>ĵ<EFBFBD>һ<EFBFBD><D2BB>Ϊ<EFBFBD>б<EFBFBD>ͷ
/////////////////////////////////////////////////////
function ADOColToRow(fromADoQry:TADOQuery;toClientSet:TclientDataSet;
chgFields:string;chgCaptions:string;
TocolCount:integer):boolean;
var
mField:string;
fieldCount:integer;
i,j,k:integer;
chgFieldsList:Tstringlist;
chgCaptionsList:TstringList;
begin
fromADoQry.DisableControls;
toClientSet.DisableControls;
try
chgFieldsList:=TStringList.Create;
chgCaptionsList:=TStringList.Create;
chgFieldsList:=split(chgFields,',');
chgCaptionsList:=split(chgCaptions,',');
/////////////////////////////////////////
toClientSet.Close;
toClientSet.FieldDefs.Clear;
//<2F><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݼ<EFBFBD>
for i:=0 to TocolCount do //
begin
toClientSet.FieldDefs.Add('def'+intTostr(i),
ftString,1000);
end;
toClientSet.Close;
toClientSet.CreateDataSet;
////////////////////////////////////////////
///////////////////////////
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD>
for i:=0 to chgCaptionsList.Count-1 do
begin
with toClientSet do
begin
Append;
//<2F>б<EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>
fieldByName('def0').Value := chgCaptionsList.Strings[i];
//<2F><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ֵ
//<2F><>ȡԪ<C8A1><D4AA><EFBFBD>ݼ<EFBFBD><DDBC>ж<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
mField:=chgFieldsList.Strings[i];
fromADoQry.first;
for j:=1 to TocolCount do
begin
//<2F><><EFBFBD><EFBFBD>ֵ
K:=0;
while not fromADoQry.Eof do
begin
fieldByName('def'+intTostr(k+1)).value:=fromADoQry.fieldByName(mField).Value ;
K:=k+1;
fromADoQry.next;
end;
end;
Post;
end;
end;
chgFieldsList.Free;
chgCaptionsList.Free ;
fromADoQry.EnableControls;
toClientSet.EnableControls;
result:=true;
except
fromADoQry.EnableControls;
toClientSet.EnableControls;
chgFieldsList.Free;
chgCaptionsList.Free ;
result:=false;
end;
end;
end.