V
violetxh
Unregistered / Unconfirmed
GUEST, unregistred user!
关于 主从表操作(对象封装) 操作,请高手指正!//下面是部分代码unit MasterData_OP;interface uses Windows,Messages,SysUtils,Variants,Classes,DB,jpeg, DataUnit,Graphics,StrUtils,DBClient, ADODB,Provider; type TMasterData_OP=class(Tobject) private DataM:TDataM; public function GetBH():string; procedure CurrentRec(BH:string;list:Tstringlist); function FindAllPro(FindSQL:string) :OLEVariant; procedure InsertProRec(InsertSQL:string;S_BH:string;var cds:TClientDataSet); procedure UpdateProRec(UpdateSQL:string;S_BH:string;var cds:TClientDataSet); function FindDetail(BH:string):OLEVariant; procedure DeleteProRec(DeleteSQL:string); constructor create; destructor Destroy;override; end;implementation constructor TMasterData_OP.create; begin DataM:=TDataM.Create(nil); end; destructor TMasterData_OP.Destroy ; begin freeandnil(Datam); inherited; end; function TMasterData_OP.GetBH():string; var No :integer; Temp:string; adoq:Tadoquery; begin adoq:=TADOQuery.Create(nil); adoq.Connection:=DataM.con1; adoq.Close; adoq.SQL.Text:='select max(BH) from MS'; adoq.Open; Temp:=Rightstr(adoq.Fields[0].AsString,5); if Temp='' then Temp:='0000'; No:=strtoint(Temp)+1; result:=RightStr('0000'+inttostr(No),5); adoq.Free; end; function TMasterData_OP.FindAllPro(FindSQL:string) :OLEVariant; var adoq:Tadoquery; DSP:TDataSetProvider; begin DSP:=TDataSetProvider.Create(nil); adoq:=TADOQuery.Create(nil); adoq.Connection:=DataM.con1; adoq.Close; adoq.SQL.Text:=FindSQL; adoq.ExecSQL; DSP.DataSet:=adoq; Result:=DSP.Data; freeandnil(DSP); FreeAndNil(adoq); end; procedure TMasterData_OP.InsertProRec(InsertSQL:string;S_BH:string;var cds:TClientDataSet); var adoq_D,adoq:Tadoquery; Dsp:TDataSetProvider; begin adoq:=TADOQuery.Create(nil); adoq_D:=TADOQuery.Create(nil); adoq_D.Connection:=DataM.con1; adoq.Connection:=DataM.con1; DSP:=TDataSetProvider.Create(nil); //Master DataM.con1.BeginTrans; try adoq.Close; adoq.SQL.Text:=InsertSQL; adoq.ExecSQL; //Detail adoq_D.Close; adoq_D.SQL.Text:='select * from DT '; adoq_D.Open; cds.First; while not cds.Eof do begin cds.Edit; cds.FieldByName('BH').AsString:=S_BH; cds.Post; cds.Next; end; Dsp.DataSet:=adoq_D; cds.SetProvider(dsp); cds.ApplyUpdates(-1); DataM.con1.CommitTrans; MessageBox(0, '保存成功!', '提示', MB_OK + MB_ICONINFORMATION); except DataM.con1.RollbackTrans; MessageBox(0, '数据有误,没有保存!', '警告', MB_OK + MB_ICONWARNING); end; FreeAndNil(adoq); FreeAndNil(adoq_D); end; procedure TMasterData_OP.UpdateProRec(UpdateSQL:string;S_BH:string;var cds:TClientDataSet); var adoq_D,adoq:Tadoquery; Dsp:TDataSetProvider; begin adoq:=TADOQuery.Create(nil); adoq_D:=TADOQuery.Create(nil); adoq_D.Connection:=DataM.con1; adoq.Connection:=DataM.con1; DSP:=TDataSetProvider.Create(nil); //Master DataM.con1.BeginTrans; try adoq.Close; adoq.SQL.Text:=UpdateSQL; adoq.ExecSQL; //Detail adoq_D.Close; adoq_D.SQL.Text:='select * from DT '; adoq_D.Open; cds.First; while not cds.Eof do begin cds.Edit; cds.FieldByName('BH').AsString:=S_BH; cds.Post; cds.Next; end; Dsp.DataSet:=adoq_D; cds.SetProvider(dsp); cds.ApplyUpdates(-1); DataM.con1.CommitTrans; MessageBox(0, '修改成功!', '提示', MB_OK + MB_ICONINFORMATION); except DataM.con1.RollbackTrans; MessageBox(0, '数据有误,没有保存!', '警告', MB_OK + MB_ICONWARNING); end; FreeAndNil(adoq); FreeAndNil(adoq_D); end;//需要源码的请留下邮箱我一一发出!