我有一个与this非常相似的代码来复制TADODataSet
组件及其事件。
所以,如果我有ADODataSet1
我想创建一个新实例ADODataSet2
作为前一个组件的完全重复组件。
一切正常,但我仍然无法复制流媒体字段(ADODataSet1PollID
,ADODataSet1Title
,ADODataSet1Description
):
object ADODataSet1: TADODataSet
Connection = ADOConnection1
CursorType = ctStatic
AfterOpen = ADODataSet1AfterOpen
CommandText = 'select top 10 * from Polls'
Parameters = <>
Left = 224
Top = 40
object ADODataSet1PollID: TGuidField
FieldName = 'PollID'
FixedChar = True
Size = 38
end
object ADODataSet1Title: TWideStringField
FieldName = 'Title'
Size = 255
end
object ADODataSet1Description: TWideStringField
FieldName = 'Description'
Size = 4000
end
end
我遇到的另一个问题是,如果ADODataSet1
设置为Active=True
,那么当我致电ms.ReadComponent(Dest)
时,Active
会在之前传输 {1}}并且会引发异常“缺少连接或ConnectionString ”。在写Connection
后,如何将Active设置为False
? (解决方法是在复制之前设置ms.WriteComponent(Source)
)。
注意:我不想要clone the cursor/recordset数据集(TADODataSet.Clone),所以请不要将其视为“重复问题”。
答案 0 :(得分:0)
试试这个:
Procedure registerAllClass(CMP: TComponent);
var
I:Integer;
begin
if (CMP is TPersistent) then begin
RegisterClass(TPersistentclass(cmp.ClassType));
end;
for I:=0 to CMP.ComponentCount-1 do
registerAllClass(cmp.Components[i]);
end;
function DuplicateComponent(Component: TComponent): TComponent;
var
MemStream: TMemoryStream;
oldname:String;
begin
oldname:=component.Name;
try
registerAllClass(Component);
Component.Name:='CopyOf'+Component.Name;
MemStream := TMemoryStream.Create;
try
MemStream.WriteComponent(Component);
MemStream.Position := 0;
result:=MemStream.ReadComponent(nil);
finally
MemStream.Free;
end;
finally
Component.Name:=oldname;
end;
end;
function DuplicateDataset(Dataset:TDataset):TDataset;
var
oldActive:Boolean;
begin
if Dataset=nil then
result:=nil
else begin
oldActive:=Dataset.Active;
try
Dataset.Active:=false;
result:=DuplicateComponent(Dataset) as TDataSet;
finally
Dataset.Active:=oldActive;
end;
end;
end;