枚举与字符串转及RecordSet转XML,JSON

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了枚举与字符串转及RecordSet转XML,JSON相关的知识,希望对你有一定的参考价值。

function AdoToJs(ado: TADOQuery): string;
var
  I, J: Integer;
  json: string;
begin
  json := \'{columns:[\';
  for I := 0 to ado.FieldCount - 1 do
  begin
    json := json + \'{name:"\' + ado.Fields[I].FieldName + \'",type:"\' + DbTypeToStr(ado.Fields[I].DataType) + \'"}\';
    if I < ado.FieldCount - 1 then
      json := json + \',\';
  end;
  json := json + \'],rows:[\';
  for I := 0 to ado.RecordCount - 1 do
  begin
    json := json + \'{\';
    for J := 0 to ado.FieldCount - 1 do
    begin
      json := json + ado.Fields[J].FieldName + \':"\' + ado.FieldByName(ado.Fields[J].FieldName).AsString + \'"\';
      if J < ado.FieldCount - 1 then
        json := json + \',\';
    end;
    json := json + \'}\';
    if I < ado.RecordCount - 1 then
      json := json + \',\';
  end;
  json := json + \']}\';
  Result := json;
end;

function AdoToXml(ado: TADOQuery): string;
begin
  Result := RecordsetToXMLString(ado.Recordset);
end;

function XmlToAdo(xml: string): TADOQuery;
var
  ado: TADOQuery;
begin
  ado := TADOQuery.Create(nil);
  ado.Recordset := RecordsetFromXMLString(xml);
  Result := ado;
end;

function RecordsetToXMLString(const Recordset: ADOInt._Recordset): string;
var
  RS: Variant;
  Stream: TStringStream;
begin
  Result := \'\';
  if Recordset = nil then
    Exit;
  Stream := TStringStream.Create(\'\', TEncoding.UTF8);
  try
    RS := CreateOleObject(\'ADODB.Recordset\');
    RS := Recordset;
    RS.Save(TStreamAdapter.Create(Stream) as IUnknown, adPersistXML);
    Stream.Position := 0;
    Result := Stream.DataString;
  finally
    Stream.Free;
  end;
end;

function RecordsetFromXMLString(const xml: string): ADOInt._Recordset;
var
  RS: Variant;
  Stream: TStringStream;
begin
  Result := nil;
  if xml = \'\' then
    Exit;
  try
    Stream := TStringStream.Create(xml, TEncoding.UTF8);
    Stream.Position := 0;
    RS := CreateOleObject(\'ADODB.Recordset\');
    RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
    Result := IUnknown(RS) as ADOInt._Recordset;
  finally
    Stream.Free;
  end;
end;

function DbTypeToStr(dbtype: TFieldType): string;
begin
  Result := GetEnumName(TypeInfo(TFieldType), Ord(dbtype))
end;

function StrToDbType(const dbtype: string): TFieldType;
begin
  Result := TFieldType(GetEnumValue(TypeInfo(TFieldType), dbtype));
end;

 

 

http://www.cnblogs.com/toosuo/archive/2012/02/18/2357315.html

以上是关于枚举与字符串转及RecordSet转XML,JSON的主要内容,如果未能解决你的问题,请参考以下文章

ADO Recordset 对象中的updateBatch函数的使用方法(转)

第一行Kotlin系列Intent隐式显式跳转及向下传值

第一行Kotlin系列Intent隐式显式跳转及向下传值

三、Uni-app + vue3 页面如何跳转及传参?

ReactNative进阶(四十六):页面跳转及传参

flutter页面跳转及返回