一个三层结构的问题,请各位高手帮帮忙???(50分)

  • 主题发起人 主题发起人 haifeilong
  • 开始时间 开始时间
H

haifeilong

Unregistered / Unconfirmed
GUEST, unregistred user!
我用SocketConnection作三层的客户端连接,由于客户端的数据要提交更新到服务端,只能一个DatasetProvider对应一个Clientdataset,有没有什么办法将一个DatasetProvider对应多个Clientdataset,或者直接在服务端定义函数用于返回客户的数据集,再将数据更新到服务端。。。各位帮帮忙。帮我解决这个问题,谢谢了。。。请给我一些这方面的代码或建议!谢谢了。。。。
 
运行时设置Clientdataset的ProviderName
 
一个DatasetProvider对应多个Clientdataset
你可以设置Clientdataset.ProviderName:= 你的服务提供者名称.
动态的修改这个值即可.
 
在应用层的数据模块中多建几个DatasetProvider就可以了。
 
写函数返回值为datasetprovide.data就可以实现一对多了
 
同意楼上的,写个变量把datasetprovide.data返回来给Clientdataset.data就直接OK了
 
是的,用函数传回DatasetProvider.Data,然后赋值给ClientDataset.Data就可以了.
更新的时候把Clientdataset.Delta传到服务器端,利用DatasetProvider.ApplyUpdate方法向数据提交更新数据.
 
我后面有类似DSP“池”的说明及源码。你可以参考。
 
procedure TRDMCardServer.SelectData(const sSQL: WideString;
out pPrvName: WideString);
safecall;
var
ADOTemp: TADODataSet;
i_iCount, i_iTag: Integer;
begin
ADOTemp := ADODS40;
i_iTag := 0;
with frmServerdo
begin
if GetDBConnectionState <> 0 then
Exit;
end;

for i_iCount := 1 to pDSCountdo
begin
ADOTemp := TADODataSet(FindComponent('ADODS' + IntToStr(i_iCount)));
if ADOTemp.Active then
begin
Continue;
end
else
begin
i_iTag := i_iCount;
Break;
end;
end;

if i_iTag = 0 then
Exit;
with ADOTempdo
begin
CommandText := sSQL;
try
Open;
pPrvName := 'DSP' + Trim(IntToStr(i_iTag));
except
pPrvName := '';
Close;
end;
end;
end;

procedure TRDMCardServer.ExecuteData(const sSQL: WideString;
out pResult: WideString;
out pMsg: WideString);
safecall;
safecall;
var
ADOComm: TADOCommand;
begin
Inc(i_CMDOrder);
if i_CMDOrder = 20 then
i_CMDOrder := 1;
ADOComm := TADOCommand(FindComponent('ADOComm' + IntToStr(i_CMDOrder)));
with frmServerdo
begin
if GetDBConnectionState <> 0 then
Exit;
end;

try
ADOComm.CommandText := sSQL;
ADOComm.Execute;
pResult := '1';
pMsg := '数据提交成功!';
except
pResult := '0';
pMsg := '数据提交失败,请检查错误后重新提交!';
end;
end;
 
客户端调用如下:
type TRDMDataSet = class(TPersistent)
//封装之后的持久层方法调用:
private
IDLLDataPer: IDLLDataPersistent;
DCOMConn: TDCOMConnection;
SocketConn: TSocketConnection;
Adapter: TDAOAdapter;
tmpCDS: TClientDataSet;
published
function begin
Trans(out pMsg: WideString): Boolean;
safecall;
function CommitTrans(out pMsg: WideString): Boolean;
safecall;
function RollBackTrans(out pMsg: WideString): Boolean;
safecall;
procedure CloseClientDS(var DataSet: TClientDataSet);
safecall;
Procedure CloseRDMDS(var DataSet: TClientDataSet);
safecall;
public
function GetMaxCount(const obj: TDataTransferObject): Integer;
overload;
safecall;
function GetMaxCount(const obj: TDataTransferObject;
filtrateSQL: String): Integer;
overload;
safecall;
function SelectRDMDS(const SqlStr: WideString;
var DataSet: TClientDataSet): Boolean;
overload;
safecall;
function SelectRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet): Boolean;
overload;
safecall;
function SelectRDMDS(const obj: TDataTransferObject;
filtrateSQL: String;
var DataSet: TClientDataSet): Boolean;
overload;
safecall;

function InsertRDMDS(const SqlStr: WideString;
out pMsg: WideString): Boolean;
overload;
safecall;
function InsertRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet;
out pMsg: WideString): Boolean;
overload;
safecall;
function InsertRDMDS(const obj: TDataTransferObject;
out pMsg: WideString): Boolean;
overload;
safecall;
function UpdateRDMDS(const SqlStr: WideString;
out pMsg: WideString): Boolean;
overload;
safecall;
function UpdateRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet;
out pMsg: WideString): Boolean;
overload;
safecall;
function UpdateRDMDS(const obj: TDataTransferObject;
out pMsg: WideString): Boolean;
overload;
safecall;

function DeleteRDMDS(const SqlStr: WideString;
out pMsg: WideString): Boolean;
overload;
safecall;
function DeleteRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet;
out pMsg: WideString): Boolean;
overload;
safecall;
function DeleteRDMDS(const obj: TDataTransferObject;
out pMsg: WideString): Boolean;
overload;
safecall;

constructor Create(pDCOMConn: TDCOMConnection);
overload;
constructor Create(pSocketConn: TSocketConnection);
overload;
end;
 
function TRDMDataSet.SelectRDMDS(const obj: TDataTransferObject;
filtrateSQL: String;
var DataSet: TClientDataSet): Boolean;
safecall;
var
i, j: integer;
begin
Result := SelectRDMDS(Adapter.getSearchSQLString(obj)+filtrateSQL, DataSet);
CloseRDMDS(DataSet);
for i:=0 to DataSet.FieldCount-1do
DataSet.Fields.DisplayLabel := obj.GridInfo;
for i:=0 to obj.GridVisible.Count-1do
begin
for j:=0 to DataSet.FieldCount-1do
if UpperCase(Trim(DataSet.Fields[j].FieldName))= UpperCase(Trim(obj.GridVisible.Strings)) then
DataSet.Fields[j].Visible := false;
end;

end;

function TRDMDataSet.SelectRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet): Boolean;
safecall;
var
i, j: integer;
begin
Result := SelectRDMDS(Adapter.getSearchSQLString(obj), DataSet);
CloseRDMDS(DataSet);
for i:=0 to DataSet.FieldCount-1do
DataSet.Fields.DisplayLabel := obj.GridInfo;
for i:=0 to obj.GridVisible.Count-1do
begin
for j:=0 to DataSet.FieldCount-1do
if UpperCase(Trim(DataSet.Fields[j].FieldName))= UpperCase(Trim(obj.GridVisible.Strings)) then
DataSet.Fields[j].Visible := false;
end;
end;

function TRDMDataSet.SelectRDMDS(const SqlStr: WideString;
var DataSet: TClientDataSet): Boolean;
var
sPrvName: WideString;
begin
Result := False;
sPrvName := '';
DataSet.Close;
DataSet.RemoteServer := SocketConn;

try
if IDLLDataPer.SelectData(@SqlStr, sPrvName) then
begin
DataSet.ProviderName := sPrvName;
try
DataSet.Close;

DataSet.Open;
Result := true;
except
Result := false;
end;
end
else
begin

if IDLLDataPer.SelectData(@SqlStr, sPrvName) then
begin
DataSet.ProviderName := sPrvName;
try
DataSet.Close;
DataSet.Open;
Result := true;
except
Result := false;
end;
end;
end;
except
Raise;
end;

if Copy(Trim(sPrvName),1,3) = 'DSP' then
CloseRDMDS(DataSet);
end;

function TRDMDataSet.InsertRDMDS(const SqlStr: WideString;
out pMsg: WideString): Boolean;
begin
Result := IDLLDataPer.InsertDataSet(@SqlStr, pMsg);
end;

function TRDMDataSet.InsertRDMDS(const obj: TDataTransferObject;
out pMsg: WideString): Boolean;
begin
SelectRDMDS(Adapter.getKeySQLString(obj), tmpCDS);
//主键更新失败
if tmpCDS.Fields[0].Value>0 then
begin
pMsg := '主键冲突,插入操作失败!';
exit;
end;

Result := InsertRDMDS(Adapter.getInsertSQLString(obj), pMsg);
end;

function TRDMDataSet.InsertRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet;
out pMsg: WideString): Boolean;
var
i: integer;
tmpPropList: TMPropList;
begin
SelectRDMDS(Adapter.getKeySQLString(obj), tmpCDS);
//主键更新失败
if tmpCDS.Fields[0].Value>0 then
begin
pMsg := '主键冲突,插入操作失败!';
DataSet.Cancel;
exit;
end;

Result := InsertRDMDS(Adapter.getInsertSQLString(obj), pMsg);
if Result = false then
begin
pMsg := '异常数据,插入操作失败!';
DataSet.Cancel;
exit;
end;

tmpPropList := obj.FPropList;
DataSet.Cancel;
DataSet.Append;

for i:=0 to tmpPropList.PropCount-1do
begin
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetOrdProp(obj, tmpPropList.Props);
tkInt64:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetInt64Prop(obj, tmpPropList.Props);
tkChar, tkLString, tkString:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetStrProp(obj, tmpPropList.Props);
tkSet:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetSetProp(obj, tmpPropList.Props);
tkEnumeration:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetEnumProp(obj, tmpPropList.Props) ;

tkFloat:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetFloatProp(obj, tmpPropList.Props) ;
tkWChar, tkWString:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetWideStrProp(obj, tmpPropList.Props) ;
end;

end;
DataSet.Post;
end;

function TRDMDataSet.UpdateRDMDS(const SqlStr: WideString;
out pMsg: WideString): Boolean;
begin
Result := IDLLDataPer.UpdateDataSet(@SqlStr, pMsg);
end;

function TRDMDataSet.UpdateRDMDS(const obj: TDataTransferObject;
out pMsg: WideString): Boolean;
var
i: integer;
tmpPropList: TMPropList;
begin
SelectRDMDS(Adapter.getKeySQLString(obj), tmpCDS);
//主键更新失败
if not tmpCDS.Fields[0].Value>0 then
begin
pMsg := '主键不能正确定位,更新操作失败!';
exit;
end;

Result := UpdateRDMDS(Adapter.getUpdateSQLString(obj), pMsg);
end;

function TRDMDataSet.UpdateRDMDS(const obj: TDataTransferObject;
var DataSet: TClientDataSet;
out pMsg: WideString): Boolean;
var
i: integer;
tmpPropList: TMPropList;
begin
SelectRDMDS(Adapter.getKeySQLString(obj), tmpCDS);
//主键更新失败
if not tmpCDS.Fields[0].Value>0 then
begin
pMsg := '主键不能正确定位,更新操作失败!';
DataSet.Cancel;
exit;
end;

Result := UpdateRDMDS(Adapter.getUpdateSQLString(obj), pMsg);

if Result = false then
exit;
tmpPropList := obj.FPropList;
if not (DataSet.State in [dsEdit]) then
DataSet.Edit;
for i:=0 to tmpPropList.PropCount-1do
begin
case tmpPropList.Props^.PropType^.Kind of
tkInteger:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetOrdProp(obj, tmpPropList.Props);
tkInt64:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetInt64Prop(obj, tmpPropList.Props);
tkChar, tkLString, tkString:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetStrProp(obj, tmpPropList.Props);
tkSet:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetSetProp(obj, tmpPropList.Props);
tkEnumeration:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetEnumProp(obj, tmpPropList.Props) ;

tkFloat:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetFloatProp(obj, tmpPropList.Props) ;
tkWChar, tkWString:
if Adapter.GetFldName(tmpPropList.PropNames)<>'' then
DataSet.FieldByName(Adapter.GetFldName(tmpPropList.PropNames)).Value := GetWideStrProp(obj, tmpPropList.Props) ;
end;

end;
DataSet.Post;
end;
 
后退
顶部