博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ADO特有的流化和还原
阅读量:5034 次
发布时间:2019-06-12

本文共 3062 字,大约阅读时间需要 10 分钟。

ADO特有的流化和还原 

{*******************************************************}

{ }
{ ADO 数据流化 }
{ }
{ 版权所有 (C) 2013 YangYxd }
{ }
{*******************************************************}

{ ado数据集流化

Rs: TADOQuery;
M: TMemoryStream;
Rs.SQL.Text := 'Select * from TBApp';
Rs.Open;
Rs.First;
M := TMemoryStream.Create;
YxdAdoStream.DataSetToStream(Rs, M);
m.SaveToFile(SoftPath + 'tbapp.data');
}

{ 从流中还原ADO数据集

Rs := TADOQuery.Create(Self);
try
t1 := GetTickCount;
M.Position := 0;
YxdAdoStream.StreamToDataSet(M, Rs);
t1 := GetTickCount - t1;
finally
FreeAndNil(M);
Rs.Free;
end;
}

unit YxdAdoStream;

interface

{$IF RTLVersion>=24}

{$LEGACYIFEND ON}
{$IFEND}

{$IF defined(FPC)}

{$DEFINE USEINLINE}
{$IFEND}
{$IF RTLVersion>=18}
{$DEFINE USEINLINE}
{$IFEND}

uses
Windows, Classes, Sysutils, comobj, ActiveX,
{$IFDEF USEINLINE}Ole2, {$ENDIF}
adoint, adodb, db;

{$IFNDEF USEINLINE}

const
IID_IPersistStream: TGUID = (
D1:$00000109;D2:$0000;D3:$0000;D4:($C0,$00,$00,$00,$00,$00,$00,$46));
IID_IStream: TGUID = (
D1:$0000000C;D2:$0000;D3:$0000;D4:($C0,$00,$00,$00,$00,$00,$00,$46));

{$ENDIF}

function CheckADODataSet(const ADataSet: TDataSet): TCustomADODataSet;

/// <summary>
/// 从流中加载数据集对象
/// </summary>
procedure StreamToDataSet(AStream: TStream; ADataSet: TCustomADODataSet);
/// <summary>
/// 将数据集写入流中
/// </summary>
procedure DataSetToStream(ADataSet: TCustomADODataSet; AStream: TStream);

implementation

resourcestring

SInvalidDataSet = '参数DataSet必须是AdoDataSet';

function CheckADODataSet(const ADataSet: TDataSet): TCustomADODataSet;

begin
if not (ADataSet is TCustomADODataSet) then
raise Exception.Create(SInvalidDataSet)
else
Result := TCustomADODataSet(ADataSet);
end;

procedure DataSetToStream(ADataSet:TCustomADODataSet; AStream:TStream);

var
ATemp: TStreamAdapter;
ADataSetStream: IPersistStream;
AIntf: IStream;
ARecs: OleVariant;
ASet: _Recordset;
begin
ASet := ADataSet.Recordset;
while (ASet.State = adStateClosed) do begin //如果执行存储过程一类的脚本,可能存在多个结果集
ASet := ADataSet.Recordset.NextRecordset(ARecs);
if ASet = nil then
raise Exception.Create('数据集无数据');
end;
OleCheck(ASet.QueryInterface(System.PGuid(@IID_IPersistStream)^, ADataSetStream));
ATemp := TStreamAdapter.Create(AStream);
try
ATemp.GetInterface(System.PGuid(@IID_IStream)^, AIntf);
OleCheck(OleSaveToStream(ADataSetStream, AIntf));
finally
ASet._Release;
ATemp.FreeInstance;
AIntf := nil;
end;
end;

procedure StreamToDataSet(AStream:TStream; ADataSet: TCustomADODataSet);

var
ATemp: Classes.TStreamAdapter;
ARecordSet: ADOInt.Recordset;
AIntf: IStream;
begin
ATemp := Classes.TStreamAdapter.Create(AStream);
try
ADataSet.LockType := ltBatchOptimistic;
ADataSet.Recordset := nil;
try
ATemp.GetInterface(System.PGuid(@IID_IStream)^, AIntf);
ComObj.OleCheck({$IFDEF USEINLINE}Ole2.{$ENDIF}OleLoadFromStream(AIntf,
{$IFDEF USEINLINE}Ole2.{$ENDIF}PGuid(@AdoInt.IID__Recordset)^, ARecordset));
ADataSet.Recordset := ARecordSet;
except
OutputDebugString(PChar(Exception(ExceptObject).Message));
end;
finally
ATemp.FreeInstance;
AIntf := nil;
end;
end;

end.

转载于:https://www.cnblogs.com/hnxxcxg/p/8663274.html

你可能感兴趣的文章
C#微信登录-手机网站APP应用
查看>>
HTML5实践 -- iPhone Safari Viewport Scaling Bug
查看>>
一位数据挖掘成功人士 给 数据挖掘在读研究生 的建议
查看>>
Python3.6.0安装
查看>>
hdu1049
查看>>
H5项目常见问题及注意事项
查看>>
索尼(SONY) SVE1512S7C 把WIN8降成WIN7图文教程
查看>>
时间模块 && time datetime
查看>>
jquery自动生成二维码
查看>>
spring回滚数据
查看>>
新浪分享API应用的开发
查看>>
美国专利
查看>>
【JavaScript】Write和Writeln的区别
查看>>
百度编辑器图片在线流量返回url改动
查看>>
我对你的期望有点过了
查看>>
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>