枚舉與字串轉及RecordSet轉XML,JSON

來源:互聯網
上載者:User

標籤:record   html   ado   save   begin   obj   data   result   nbsp   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.