主表为ecsms_customer,细表为ecsms_stockprice, 关联字段为customer_code,FMasterds为主表数据集, Fquery为细表数据集,FConn为TADOConnectionAdd操作: procedure TCustomer_DM.AddCustomer; begin inherited; if not CanAddCustomer(FCustomer.CustomerCode) then begin MessageDlg(FCustomer.CustomerCode+'已经存在!',mtWarning,[mbOK],0); exit; end; with FMasterds do begin Append; FieldByName('customer_code').Value:=FCustomer.CustomerCode; FieldByName('company_name').Value:=FCustomer.CompanyName; FieldByName('address').Value:=FCustomer.Address; FieldByName('postcode').Value:=FCustomer.Postcode; end; with Fquery do begin SQL.Clear; SQL.Text:='select * from ECSMS_Stockprice'; open; Append; FieldByName('customer_code').Value:=FStockprice.CustomerCode; FieldByName('unit_price').Value:=FStockprice.UnitPrice; FieldByName('E20Lift').Value:=FStockprice.E20Lift; FieldByName('E20Stock').Value:=FStockprice.E20Stock; FieldByName('G20Lift').Value:=FStockprice.G20Lift; FieldByName('G20Stock').Value:=FStockprice.G20Stock; FieldByName('E40Lift').Value:=FStockprice.E40Lift; FieldByName('E40Stock').Value:=FStockprice.E40Stock; end; if not FConn.InTransaction then FConn.BeginTrans; try FMasterds.Post; Fquery.Post; FConn.CommitTrans; except FConn.RollbackTrans; Messagedlg('操作失败!',mtwarning,[mbOK],0); end; Fquery.Close;
Delete操作: procedure TCustomer_DM.DeleteCustomer; var tempstr:string; begin inherited; if FMasterds.Eof then exit; if Messagedlg('真的要删除该记录?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin tempstr:=FMasterds.Fields.Fields[0].AsString; //Fields[0]为customer_code字段 Fquery.Prepared:=true; Fquery.SQL.Clear; Fquery.SQL.Text:='delete from ECSMS_stockprice where customer_code='+''''+tempstr+''''; if not FConn.InTransaction then FConn.BeginTrans; try Fquery.ExecSQL; FMasterds.Delete; FConn.CommitTrans; except FConn.RollbackTrans; Messagedlg('删除操作失败!',mtwarning,[mbOK],0); end; end; end;
修改操作: procedure TCustomer_DM.ModifyCustomer; begin inherited; if not FMasterds.Modified and not FDetailds.Modified then exit; if Messagedlg('保存修改?',mtConfirmation,[mbYes,mbNo],0)=mrNo then begin if FMasterds.Modified then FMasterds.Cancel; if FDetailds.Modified then FDetailds.Cancel; exit; end; if not FConn.InTransaction then FConn.BeginTrans; try if FMasterds.Modified then FMasterds.Post; FDetailds.Edit; FDetailds.FieldByName('customer_code').AsString:=FMasterds.Fields.Fields[0].AsString; if FDetailds.Modified then FDetailds.Post; FConn.CommitTrans; except FConn.RollbackTrans; Messagedlg('操作失败!',mtwarning,[mbOK],0); end; end;
关联字段为customer_code,FMasterds为主表数据集,
Fquery为细表数据集,FConn为TADOConnectionAdd操作:
procedure TCustomer_DM.AddCustomer;
begin
inherited;
if not CanAddCustomer(FCustomer.CustomerCode) then
begin
MessageDlg(FCustomer.CustomerCode+'已经存在!',mtWarning,[mbOK],0);
exit;
end; with FMasterds do
begin
Append;
FieldByName('customer_code').Value:=FCustomer.CustomerCode;
FieldByName('company_name').Value:=FCustomer.CompanyName;
FieldByName('address').Value:=FCustomer.Address;
FieldByName('postcode').Value:=FCustomer.Postcode;
end; with Fquery do
begin
SQL.Clear;
SQL.Text:='select * from ECSMS_Stockprice';
open;
Append;
FieldByName('customer_code').Value:=FStockprice.CustomerCode;
FieldByName('unit_price').Value:=FStockprice.UnitPrice;
FieldByName('E20Lift').Value:=FStockprice.E20Lift;
FieldByName('E20Stock').Value:=FStockprice.E20Stock;
FieldByName('G20Lift').Value:=FStockprice.G20Lift;
FieldByName('G20Stock').Value:=FStockprice.G20Stock;
FieldByName('E40Lift').Value:=FStockprice.E40Lift;
FieldByName('E40Stock').Value:=FStockprice.E40Stock;
end; if not FConn.InTransaction then FConn.BeginTrans;
try
FMasterds.Post;
Fquery.Post;
FConn.CommitTrans;
except
FConn.RollbackTrans;
Messagedlg('操作失败!',mtwarning,[mbOK],0);
end;
Fquery.Close;
procedure TCustomer_DM.DeleteCustomer;
var
tempstr:string;
begin
inherited;
if FMasterds.Eof then exit; if Messagedlg('真的要删除该记录?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
begin
tempstr:=FMasterds.Fields.Fields[0].AsString;
//Fields[0]为customer_code字段
Fquery.Prepared:=true;
Fquery.SQL.Clear;
Fquery.SQL.Text:='delete from ECSMS_stockprice where customer_code='+''''+tempstr+'''';
if not FConn.InTransaction then FConn.BeginTrans;
try
Fquery.ExecSQL;
FMasterds.Delete;
FConn.CommitTrans;
except
FConn.RollbackTrans;
Messagedlg('删除操作失败!',mtwarning,[mbOK],0);
end;
end;
end;
procedure TCustomer_DM.ModifyCustomer;
begin
inherited;
if not FMasterds.Modified and not FDetailds.Modified then exit; if Messagedlg('保存修改?',mtConfirmation,[mbYes,mbNo],0)=mrNo then
begin
if FMasterds.Modified then FMasterds.Cancel;
if FDetailds.Modified then FDetailds.Cancel;
exit;
end;
if not FConn.InTransaction then FConn.BeginTrans;
try
if FMasterds.Modified then FMasterds.Post;
FDetailds.Edit;
FDetailds.FieldByName('customer_code').AsString:=FMasterds.Fields.Fields[0].AsString;
if FDetailds.Modified then FDetailds.Post;
FConn.CommitTrans;
except
FConn.RollbackTrans;
Messagedlg('操作失败!',mtwarning,[mbOK],0);
end;
end;