在delphi中三个形式:ADODB_TLB ADOInt ADODB
时间:2010-08-31 来源:覆雨翻云
转自:http://www.mini188.com/showtopic-862.aspx
在delphi中三个形式:ADODB_TLB ADOInt ADODB
RecordSet在delphi中常见的转换问题
三个文件中都有,但都不同ADODB_TLB._RecordSet ADOInt._RecordSet ADODB.RecordSet
ADODataSet和ADOQuery只能接受ADOInt._RecordSet,不能接受ADODB_TLB._RecordSet
原生_RecordSet与ADOInt._RecordSet互转,应该可以实现,但我没有去研究。
【第一种应用】RecordSet与OleVariant互转,uses ADODB_TLB,不能用ro的binaryfromvariant,因为这是xml纯广本,不是olevariant 纯文本xml转olevariant会失真。需要用另行处理。
【第二种应用】RecordSet与Binary互转,uses uROTypes uROBinaryHelpers
【第三种应用】RecordSet与String互转,uses ADODB(delpih7自带的)
要点:
1.RecordSet转为String后,再由String变为OleVariant,转回RecordSet,会报错。
2.ADODataSet.RecordSet的值,可以用ADODB.RecordSet转来的RS,但不能用ADODB_TLB._RecordSet
【原生 ADO 与 Delphi ADOExpress 组件的对应关系】
1、Connection <=> ADOConnection.ConnectionObject;
2、Recordset <=> ADODataSet.Recordset;
3、Command <=> ADOCommand.CommandObject;
4、? <=> ADOQuery,因为 ADOQuery 根本就不是原生 ADO 对象
5、ExecSQL <=> ADODataSet.Open;
6、ExecSQLA <=> ADOCommand.Execute;
【第一种应用】RecordSet与OleVariant互转
function SetAdoStream(RDS : ADODB._RecordSet) : OleVariant;
var
Objstream : ADODB_TLB._Stream;
ObjRs1 : ADODB_TLB._RecordSet;
ObjRs2 : OleVariant;
begin
try
Objstream:=CoStream.Create;
ObjRs1:=ADODB_TLB.CoRecordset.Create;
ObjRs2:=CreateOleObject('ADODB.RecordSet');
ObjRs1:=ADODB_TLB._RecordSet(RDS);
ObjRs1.Save(ObjStream,adPersistXML);
ObjRs2.Open(ObjStream);
Result:=ObjRs2;
ObjRs1.Close;
ObjRs1:=nil;
ObjRs2:=NULL;
ObjStream:=nil;
except
Result:=NULL;
ObjRs1.Close;
ObjRs1:=nil;
ObjRs2:=NULL;
ObjStream:=nil;
end;
end;
function GetAdoStream(oStream : OleVariant) : ADODB._Recordset;
begin
Result:=IDispatch(oStream) As ADODB._RecordSet;
end;
【第二种应用】RecordSet与Binary互转
function RecordsetToTms(const Recordset: ADODB._Recordset; var Stream: Binary): boolean;
var
RS: OleVariant;
begin
Result := false;
if Recordset = nil then Exit;
Stream:=Binary.Create;
try
RS := CreateOleObject('ADODB.Recordset');
RS := Recordset;
RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistADTG);
Stream.Position := 0;
Result := true;
except
on E:Exception do
begin
RS:=E.Message;
Stream:=BinaryFromVariant(RS);
Result:=False;
end;
end;
end;
function RecordsetFromTms(Stream: Binary): ADODB._Recordset;
var
RS: OleVariant;
begin
Result := nil;
if Stream.Size < 1 then Exit;
try
Stream.Position := 0;
RS := CreateOleObject( 'ADODB.Recordset');
RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
Result := IUnknown(RS) as ADODB._Recordset;
finally;
end;
end;
【第三种应用】RecordSet与String互转
function RecordsetToXMLString(const Recordset: adoint._Recordset): string;
var
RS: Variant;
Stream: TStringStream;
begin
Result :='';
if Recordset = nil then Exit;
Stream := TStringStream.Create('');
try
RS := CreateOleObject('ADODB.Recordset');
RS := Recordset;
RS.Save(TStreamAdapter.Create(stream) as IUnknown, adPersistXML);
Stream.Position := 0;
result := Stream.DataString;
finally
Stream.Free;
end;
end;
function RecordsetFromXMLString(const XML: string): adoint._Recordset;
var
RS: Variant;
Stream: TStringStream;
begin
Result := nil;
if XML = '' then Exit;
try
Stream := TStringStream.Create(XML);
Stream.Position := 0;
RS := CreateOleObject('ADODB.Recordset');
RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
Result := IUnknown(RS) as adoint._Recordset;
finally
Stream.Free;
end;
end;
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/delphi1234/archive/2009/06/12/4264472.aspx