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