枚举与字符串转及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;

转载于:https://www.cnblogs.com/toosuo/archive/2012/02/18/2357315.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值