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.

最新文章

  1. 一个快速排序(分类)及使用类似思想实现选择问题[c++实现]
  2. Java读取Level-1行情dbf文件极致优化(1)
  3. 一款免费的js图表工具--morris
  4. windows10, 安装wamp无法启动服务的问题
  5. C++程序设计之四书五经[转自2004程序员杂志]--下篇
  6. 数迹学——Asp.Net MVC4入门指南(4):添加一个模型
  7. css伪类运用
  8. Java容器类接口的选择
  9. Linux中的free命令
  10. C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
  11. CSS skills: 4) goto page head script
  12. Activity的启动模式及回退栈的概念
  13. 【ADO.NET】8、DataSet的使用
  14. 开始hadoop
  15. LeetCode_Distinct Subsequences
  16. AIX-vi操作-提示Unknown terminal type的问题解决方法
  17. java使用注解和反射打造一个简单的jdbc工具类
  18. 【转】Java 工程师成神之路
  19. python+NLTK 自然语言学习处理二:文本
  20. Android 开发笔记___复选框__checkbox

热门文章

  1. java的应用项目
  2. caffe Python API 之Dropout
  3. UML基础
  4. echo常用操作
  5. 初学jmeter
  6. 深度学习方法:受限玻尔兹曼机RBM(四)对比散度contrastive divergence,CD
  7. redis之(十八)redis的支持水平扩容的集群特性,以及插槽的相关操作
  8. Binary Tree Preorder Traversal——经典算法的迭代求解(前序,中序,后序都在这里了)
  9. 【转载】Web开发技术发展历史-版本2
  10. 如何实现artTemplate模板的可重用性,以此框架打造自己的自定义组件