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.

ADO特有的流化和还原的更多相关文章

  1. c#无标题窗体点击任务栏图标正常最小化或还原

    FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还原的. protecte ...

  2. C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法

    对于无标题栏窗体,也就是FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还 ...

  3. iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. vlc命令行: 转码 流化 推流

    vlc命令行: 转码 流化 推流 写在命令行之前的话: VLC不仅仅可以通过界面进行播放,转码,流化,也可以通过命令行进行播放,转码和流化.还可以利用里面的SDK进行二次开发. vlc命令行使用方法: ...

  5. mp4流化

    MP4需要流化 不然会频繁seek 对于http形式的播放而言 苦不堪言 ffmpeg -i g:/media/err.mp4 -movflags +faststart -codec copy g:/ ...

  6. IM系统-消息流化一些常见问题

    原创不易,求分享.求一键三连 之前说过IM系统的一些优化,但是在网络上传输数据对于数据的流化和反流化也是处理异常情况的重点环节,不处理好可能会出现一些消息发送成功,但是解析失败的情况,本文就带大家来一 ...

  7. winform窗体最大化、最小化、还原

    //最大化 private void button_maxsize_Click(object sender, EventArgs e)        {            this.WindowS ...

  8. layer最大化、最小化、还原回调方法

    layer.open({            type: 1,             title: ‘在线调试‘,            content: ‘这里是内容‘,            ...

  9. C# 实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法

    /// <summary> /// 实现窗体的最小化 /// </summary> protected override CreateParams CreateParams { ...

随机推荐

  1. golang中 return如果返回指针比大型struct性能高

    type tt struct{ aa int bb int cc int str string } func func_rstruct () tt{ t:=tt{1,2,3,"8888888 ...

  2. C基础 内存越界和内存监测的简单处理

    引言 突然感觉要出去走走了, 醒了后 刷完牙就在联系coding, 不知不觉到了 黄昏. 看看天, 打开灯. 又感觉到了 夜夜夜夜 . 13年到北京务工, 遇到一批批NB的同龄人物. 一块工作, 一块 ...

  3. java数组面试题

    一维数组可以写成:int[ ]x 或者int x[ ]: 二维数组可以写成:int[ ] y [ ] 或者int y[ ][ ] 或者int [ ][ ]y 面试题如下:       声明数组int[ ...

  4. Unknown character set: 'utf8mb4'

    出现Unknown character set: 'utf8mb4'该错误是因为你的mysql-connector-java版本太高了,现在的mysql编码方式utf8mb4  然而老版本的却是utf ...

  5. django的事务

    在某些时候,你可能会在视图修改两张数据表.并且想让他们同时成功或者同时失败.这就是事务的原子性(atomicity).在django中应该怎么做呢? 详细可以参考官方文档:https://yiyibo ...

  6. linux命令(38):traceroute命令

    1.命令格式: traceroute[参数][主机] 2.命令功能: traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes,用户可另行设置. 具体参数格式:tracer ...

  7. 181. Employees Earning More Than Their Managers

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

  8. K8S的APISERVER,应用了HTTPS之后,命令行如何访问?

    用命令行总是很麻烦,因为要自定义一些证书的位置....... curl https://1.2.3.1:443/api/v1/nodes \ --cacert /etc/kubernetes/pki/ ...

  9. Vue.js—组件快速入门及Vue路由实例应用

    上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...

  10. Oracle 数据库分页查询的三种方法

    一.Oracle 数据库分页查询的三种方法 1.简介 不能对 rownum 使用 >(大于或等于 1 的数值).>=(大于 1 的数值).=(不等于 1 的数值),否则无结果.所以直接用 ...