枚举与字符串转及RecordSet转XML,JSON
2024-10-01 23:52:46
function AdoToJs(ado: TADOQuery): string;
var
I, J: Integer;
json: string;
begin
json := '{columns:[';
for I := to ado.FieldCount - do
begin
json := json + '{name:"' + ado.Fields[I].FieldName + '",type:"' + DbTypeToStr(ado.Fields[I].DataType) + '"}';
if I < ado.FieldCount - then
json := json + ',';
end;
json := json + '],rows:[';
for I := to ado.RecordCount - do
begin
json := json + '{';
for J := to ado.FieldCount - do
begin
json := json + ado.Fields[J].FieldName + ':"' + ado.FieldByName(ado.Fields[J].FieldName).AsString + '"';
if J < ado.FieldCount - then
json := json + ',';
end;
json := json + '}';
if I < ado.RecordCount - 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 := ;
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 := ;
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
最新文章
- HikariCP
- Sprint(第八天11.21)
- AndroidManiFast 字段意义
- Hadoop HDFS 架构设计
- 【spring 配置文件】spring配置文件的解析
- Auto push git tag
- 减少C++代码编译时间的方法
- 把Nginx加为系统服务(service nginx start/stop/restart)
- ADO.NET(很精彩全面)
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
- Ubuntu系统、开发环境配置
- JavaScript高级程序设计13.pdf
- 成都Uber优步司机快速注册攻略(外地车牌也可加入,不用现场培训)
- 无法打开物理文件mdf,操作系统错误 5:&;quot;5(拒绝訪问。)&;quot;
- OpenStack命令 创建网络和路由管理
- android调用系统相机进行视频录制并保存到指定目录
- C++11新特性之tie、tuple的应用
- Android6.0运行时权限(基于RxPermission开源库)
- appium工作原理
- airTest 使用体验
热门文章
- Cisco IOS images
- 数据局部性(data locality)
- Warning: file_put_contents(常用单词1.txt): failed to open stream: Invalid argument in
- DesignPattern_Java:SingletonPattern
- lipo: can&#39;t open input file
- Ubuntu server使用命令行上板VPNclient
- Arcgis api for javascript学习笔记(4.5版本) - 获取FeatureLayer中的graphics集合
- this解析
- 取消Jquery mobile自动省略的文本
- Why I Choose Delphi Summary