Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 3479|回复: 0

DataSet 转 MemDataSet (没有技术含量纯自娱自乐)

[复制链接]

该用户从未签到

发表于 2011-8-3 16:34:00 | 显示全部楼层 |阅读模式
Pascal下 DataSet中的内容是非离线的,就是说需要与数据库保持联机,这样很不好,会弄出很多连接出来,不喜欢。
MemDataSet,是可以保存离线内容的,于是就有了这个方法。

function TformMain.DataSet2MDataSet(dataSet:TDataSet):TMemDataSet;
var
  i:Integer;
  strColumn,strType,strSize,strValue:string;
  mDataset:TMemDataSet;
begin
  //初始化
  mDataSet:= TMemDataset.Create(nil);
  //字段
  with dataSet do
  for i := 0 to FieldCount-1 do
  begin
     strColumn:= dataSet.Fields.FieldName;
     strType:= GetEnumName(TypeInfo(TFieldType),integer(dataSet.Fields.DataType)) ;
     strSize:= IntToStr(dataSet.Fields.DataSize);
     if strColumn ='SUMNUMBER'then
     Application.MessageBox(Pchar(strType),'',0);
     if (strType ='ftString') or (strType ='ftBCD')then
        mDataSet.FieldDefs.Add(strColumn, TFieldType(GetEnumValue(TypeInfo(TFieldType), strType)),StrToInt(strSize))
     else
        mDataSet.FieldDefs.Add(strColumn, TFieldType(GetEnumValue(TypeInfo(TFieldType), strType)));
  end;
  mDataSet.CreateTable;
  mDataSet.Open;
  //数据
  dataSet.First;
  with dataSet do
    while not dataSet.eof do
     begin
       mDataSet.Append;
       for i := 0 to FieldCount-1 do
        begin
            strValue:= dataSet.Fields.AsString;
            mDataSet.Fields.AsString:=strValue;
       end;
       dataSet.Next;
     end;
  Result:=mDataSet;
end;
回复

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-5-2 08:03 , Processed in 0.038716 second(s), 9 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表