(原)Lazarus 在 DataSet 转 MemDataSet

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[i].FieldName;
     strType:= GetEnumName(TypeInfo(TFieldType),integer(dataSet.Fields[i].DataType)) ;
     strSize:= IntToStr(dataSet.Fields[i].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[i].AsString;
            mDataSet.Fields[i].AsString:=strValue;
       end;
       dataSet.Next;
     end;
  Result:=mDataSet;
end;

转载于:https://www.cnblogs.com/spymaster/archive/2011/08/03/2126370.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值