MasterDetail(100分)

  • 主题发起人 主题发起人 zyx1122
  • 开始时间 开始时间
Z

zyx1122

Unregistered / Unconfirmed
GUEST, unregistred user!
How to create Master/Detail in running time, not design time?[:(]
 
Can you explain it more? How to set master and detail's property?
Can you give a sample? Thanks.
I use TQuery(qryMaster, qryDetail) in three ties.
And Ido
n't set the qryMaster.SQL and qryDetail.SQL in design time.
I want to set them in running time. Like in from.OnCreate or datamodule.***
 
I hope the sample is like the follow:
Database : DBDEMO
Table : Customer, Order
If so, I can run it in my computer.
And I just study Delphi, so could you give more detail? Thanks.
 
qrymaster.sql := 'select * from tablemaster'
datasourcemaster.dataset := qrymaster;
qrydetaile.datasource := datasourcemaster;
qrydetaile.sql := 'select * from tabledetaile where field1 = :field1';
field1 is qrydetaile's FOREIGN KEY
 
需设定TTable的属性mastersource,index几个属性,你查一下
只需在程序中设定应该是和在设计时一样的
 
Itdo
esn't work.
Is anything else
I miss? (some property I should set or ... )
I am just studying delphi for a short time.
Can you tell me more?
 
at qrymaster.afterscroll write
qrydetail.close;
qrydetail.sql.clear;
qrydetail.sql.add('select * from orders where custno='''+
qrymaster.fieldbyname('custno').asstring+'''');
qrydetail.open;
 
Eg :
qrymaster.databasename := DBDEMOS;
qrymaster.sql.close;
qrymaster.sql.clear;
qrymaster.sql := 'select * from animals'
qrymaster.sql.open;
datasourcemaster.dataset := qrymaster;
qrydetaile.datasource := datasourcemaster;
qrydetaile.sql.close;
qrydetaile.sql.clear;
qrydetaile.sql := 'select * from animals where field1 = :field1';
qrydetaile.sql.open;
 
Sorry, everyone.
No onedo
es work.
1. Sherman: I write the code you give me in server side,
but when I scroll in client side itdo
esn't work. And I write the code in
client side, there has not the "qryDetail.SQL", I know this. I write
cdsDetail.CommandText, but the error message is "It can't change CommandText"
And another thing, why it can't trigger master's "AfterScroll" in server side
when I run "AfterScroll' in client side?
2. delphing: it can't find "field1"
Should I set field1 in somewhere?
 
Here is my code:
///////////////////// udmMDS server side DataModle /////////////////////////
unit udmMDS;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Dialogs, Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
DBClient, prjMDS_TLB, StdVcl, Provider, DB, ADODB, DBTables;
type
TmdRemoteServer = class(TRemoteDataModule, ImdRemoteServer)
dsMaster: TDataSource;
db: TDatabase;
session: TSession;
qryMaster: TQuery;
qryDetail: TQuery;
dspMaster: TDataSetProvider;
dspDetail: TDataSetProvider;
procedure qryMasterAfterScroll(DataSet: TDataSet);
private
m_strMasterSQL : string;
m_strDetailSQL : string;
m_strMasterFields : string;
protected
class procedure UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
override;
procedure SetSQL(const strMasterSQL, strDetailSQL,
strMasterFields: WideString);
safecall;
public
{ Public declarations }
end;

implementation
{$R *.DFM}
class procedure TmdRemoteServer.UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;

procedure TmdRemoteServer.SetSQL(const strMasterSQL, strDetailSQL,
strMasterFields: WideString);
begin
m_strMasterSQL := strMasterSQL;
m_strDetailSQL := strDetailSQL;
m_strMasterFields := strMasterFields;
qryMaster.Close;
qryMaster.SQL.Clear;
qryMaster.SQL.Add(m_strMasterSQL);
qryMaster.Open;
qryDetail.Close;
qryDetail.SQL.Clear;
qryDetail.SQL.Add(m_strDetailSQL);
qryDetail.Open;
end;

procedure TmdRemoteServer.qryMasterAfterScroll(DataSet: TDataSet);
var
strMasterField : string;
begin
qryDetail.Close;
qryDetail.SQL.Clear;
strMasterField := qryMaster.FieldByName('CustNo').AsString;
qryDetail.SQL.Add(m_strDetailSQL
+ ' where CustNo = "'
+ strMasterField + '"');
qryDetail.Open;
end;

initialization
TComponentFactory.Create(ComServer, TmdRemoteServer,
Class_mdRemoteServer, ciMultiInstance, tmApartment);
end.

///////////////////////// udmMDC client side DataModule //////////////////////
unit udmMDC;
interface
uses
SysUtils, Classes, DB, DBClient, MConnect, Dialogs;
type
TdmMDC = class(TDataModule)
con: TDCOMConnection;
cdsMaster: TClientDataSet;
cdsDetail: TClientDataSet;
dsMaster: TDataSource;
dsDetail: TDataSource;
procedure DataModuleCreate(Sender: TObject);
procedure cdsMasterAfterScroll(DataSet: TDataSet);
procedure DataModuleDestroy(Sender: TObject);
private
m_strMasterSQL : string;
m_strDetailSQL : string;
m_strMasterFields : string;
public
{ Public declarations }
end;

var
dmMDC: TdmMDC;
implementation
{$R *.dfm}
procedure TdmMDC.DataModuleCreate(Sender: TObject);
begin
con.Connected := true;
m_strMasterSQL := 'select * from customer';
m_strDetailSQL := 'select * from orders';
m_strMasterFields := 'CustNo';
con.AppServer.SetSQL(m_strMasterSQL, m_strDetailSQL, m_strMasterFields);
udmMDC.dmMDC.cdsMaster.Active := true;
udmMDC.dmMDC.cdsDetail.Active := true;
end;

procedure TdmMDC.cdsMasterAfterScroll(DataSet: TDataSet);
var
strMasterField : string;
begin
cdsDetail.Close;
strMasterField := cdsMaster.FieldByName('CustNo').AsString;
ShowMessage(strMasterField);

{
cdsDetail.CommandText := m_strDetailSQL
+ ' where CustNo = "'
+ strMasterField + '"';
}
cdsDetail.Open;
end;

procedure TdmMDC.DataModuleDestroy(Sender: TObject);
begin
con.Connected := false;
udmMDC.dmMDC.cdsMaster.Active := false;
udmMDC.dmMDC.cdsDetail.Active := false;
end;

end.
 
Help me!
Thanks.
 
the error 'It can't change CommandText' may be from :
on server ,the TDataSetProvider's Options/poAllowCommandText
its value is true ,not false.
try again.
 
多人接受答案了。
 
后退
顶部