zoukankan      html  css  js  c++  java
  • 枚举与字符串转及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

  • 相关阅读:
    html input type=file 选择图片,图片预览 纯html js实现图片预览
    asp.net mvc Controller控制器返回类型
    webrequest HttpWebRequest webclient/HttpClient
    js中__proto__和prototype constructor 的区别和关系
    JQuery的ajaxFileUpload的使用
    cuda中当数组数大于线程数的处理方法
    cuda中threadIdx、blockIdx、blockDim和gridDim的使用
    cuda和gcc版本不兼容
    【转】CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)
    matlab练习程序(地图上画经纬度)
  • 原文地址:https://www.cnblogs.com/findumars/p/7126915.html
Copyright © 2011-2022 走看看