(原)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;

posted @ 2011-08-03 16:35  禹过天晴  阅读(1177)  评论(0编辑  收藏  举报