Datatable/Dataset 转 JSON方法

当数据库表的数据在一般处理程序中查出来需要将这个表数据返回到前台的jquery中,需要将数据拼成json字符串形式,
这里是将数据库数据查出放在Datatable中,然后在一般处理程序中将datatable转成json的几个方法,根据不同的情况调用不同的方法(Dataset集合中的数据也可以遍历每一个索引上的datatable调用此方法)。


using
System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Text;/// <summary> ///DataHelper 的摘要说明 /// </summary> public class DataHelper {public DataHelper(){////TODO: 在此处添加构造函数逻辑// }#region DataTable转Json/// <summary>/// DataTable转Json for DataGrid/// </summary>/// <param name="_dt"></param>/// <returns></returns>public static string ToDataGrid(DataTable _dt){string GridJson = string.Empty;try{GridJson = "{\"total\":" + _dt.Rows.Count + ",\"rows\":[";for (int i = 0; i < _dt.Rows.Count; i++){GridJson += "{";string strTemp = "";for (int j = 0; j < _dt.Columns.Count; j++){strTemp = (j == _dt.Columns.Count - 1 ? "" : ",");GridJson += "\"" + _dt.Columns[j].ColumnName.ToString() + "\":\"" + _dt.Rows[i][j].ToString().Trim() + "\"" + strTemp;}strTemp = (i == _dt.Rows.Count - 1 ? "}" : "},");GridJson += strTemp;}GridJson += "]}";// Rev_DBPublic.Rev_WriteLog.WriteLog("PubErrorLogPath", "ToDataGrid" + "123", GridJson); }catch (Exception ee){Rev_DBPublic.Rev_WriteLog.WriteLog("PubErrorLogPath", "ToDataGrid" + "123", GridJson + "Err:" + ee.Message);}return GridJson;}#endregion/// <summary>/// 将Datatable 中得数据行进行分页,并将页内数据返回/// </summary>/// <param name="dt"></param>/// <param name="pageSize">行数</param>/// <param name="pageIndex">第几页</param>/// <returns></returns>public static DataTable DataTableToPager(DataTable dt, int pageSize, int pageIndex){if (dt == null || dt.Rows.Count == 0){return dt;}DataTable returnValue = null;DataColumn dc = new DataColumn("rowindex", Type.GetType("System.Int32"));dt.Columns.Add(dc);for (int i = 0; i < dt.Rows.Count; i++){dt.Rows[i]["rowindex"] = i;}int _start = (pageIndex - 1) * pageSize;int _end = _start + pageSize;DataView dv = dt.DefaultView;dv.RowFilter = string.Format("rowindex>={0} and rowindex<{1}", _start, _end);returnValue = dv.ToTable();return returnValue;}#region DataTable转Json/// <summary>/// DataTable转Json for DataGrid/// </summary>/// <param name="_dt"></param>/// <returns></returns>public static string ToDataGridPage(DataTable _dt, int _row){string GridJson = "{\"total\":" + _row + ",\"rows\":[";for (int i = 0; i < _dt.Rows.Count; i++){GridJson += "{";string strTemp = "";for (int j = 0; j < _dt.Columns.Count; j++){strTemp = (j == _dt.Columns.Count - 1 ? "" : ",");GridJson += "\"" + _dt.Columns[j].ColumnName.ToString() + "\":\"" + _dt.Rows[i][j].ToString() + "\"" + strTemp;}strTemp = (i == _dt.Rows.Count - 1 ? "}" : "},");GridJson += strTemp;}GridJson += "]}";// Rev_DBPublic.Rev_WriteLog.WriteLog("PubErrorLogPath", "ToDataGrid" + "123", GridJson);return GridJson;}#endregion#region DataTable转Json/// <summary>/// DataTable转Json for DataGrid/// </summary>/// <param name="_dt"></param>/// <returns></returns>public static string ToDataGridPage(DataTable _dt, int _row, int page, int records){string GridJson = "{\"page\":" + page + ",\"total\":" + _row + ",\"records\":" + records + ",\"rows\":[";for (int i = 0; i < _dt.Rows.Count; i++){GridJson += "{";string strTemp = "";for (int j = 0; j < _dt.Columns.Count; j++){strTemp = (j == _dt.Columns.Count - 1 ? "" : ",");GridJson += "\"" + _dt.Columns[j].ColumnName.ToString() + "\":\"" + _dt.Rows[i][j].ToString() + "\"" + strTemp;}strTemp = (i == _dt.Rows.Count - 1 ? "}" : "},");GridJson += strTemp;}GridJson += "]}";// Rev_DBPublic.Rev_WriteLog.WriteLog("PubErrorLogPath", "ToDataGrid" + "123", GridJson);return GridJson;}#endregion#region dataTable转换成Json格式/// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string DataTable2Json(DataTable dt){StringBuilder jsonBuilder = new StringBuilder();jsonBuilder.Append("[");for (int i = 0; i < dt.Rows.Count; i++){jsonBuilder.Append("{");for (int j = 0; j < dt.Columns.Count; j++){jsonBuilder.Append("\"");jsonBuilder.Append(dt.Columns[j].ColumnName);jsonBuilder.Append("\":\"");jsonBuilder.Append(dt.Rows[i][j].ToString());jsonBuilder.Append("\",");}jsonBuilder.Remove(jsonBuilder.Length - 1, 1);jsonBuilder.Append("},");}jsonBuilder.Remove(jsonBuilder.Length - 1, 1);if (dt.Rows.Count > 0){jsonBuilder.Append("]");}return jsonBuilder.ToString();}#endregion dataTable转换成Json格式 }

 

 

 

 

转载于:https://www.cnblogs.com/zyg316/p/5974167.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/270699.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库:SQLServer数据库备份方式介绍

今天小编给大家梳理一下数据库备份的相关知识&#xff0c;希望对大家能有所帮助&#xff01;1、完整备份完整备份相当于针对整个数据库备份&#xff0c;包含数据库的全部内容。数据库完整备份可以将数据库的所有内容备份为一个.Bak文件。后续可以通过该备份文件在本机或者拷贝到…

注入双括号报错注入_SQL手动注入:sqlilabs(less110)

种一棵树最好的时间是10年前&#xff0c;其次是现在。忘了是谁前言说实话&#xff0c;由于前段时间学 python &#xff0c;对于 OWASP TOP10 漏洞的学习都有所落下&#xff0c;虽然现在也在慢慢复习当中&#xff0c;但是今晚谈及的 SQL 注入漏洞手动注入和自动化(sqlmap)注入的…

try、catch、finally用法总结

一、异常的处理&#xff1a;抓抛模型过程一&#xff1a;"抛"&#xff1a;程序在正常执行的过程中&#xff0c;一旦出现异常&#xff0c;就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出对象以后&#xff0c;其后的代码就不再执行。关于异常对象…

markdown学习/mou

markdown编辑器mou markdown编辑器的使用很简单&#xff0c;mac平台选择课 MOU 这款比较轻的客户端。 使用也很方便&#xff0c;打开软件&#xff0c;->helo->mou help 就有各种示例&#xff0c;照葫芦画瓢。 有些关键字需要转义,使用的时候要注意&#xff0c;比如插入一…

硬件知识:内存条出现故障的解决方案!

“电脑不时的突然出现蓝屏”&#xff0c;“开机之后毫无规律的死机”&#xff0c;“开不了机&#xff0c;一开机就出现嘶鸣声”&#xff0c;如果你的电脑出现了以上情况&#xff0c;就是你的内存兼容性出了问题&#xff0c;那么这种故障该如何解决呢&#xff1f;下面&#xff0…

api zabbix 拓扑图 获取_zabbix网络拓扑图配置-Maps(示例代码)

zabbix network map”可以简单的理解为动态网络拓扑图&#xff0c;可以针对业务来配置zabbix map&#xff0c;通过map可以了解应用的整体状况&#xff1a;服务器是否异常、网络是否有故障、应用当前什么状态。如果你不需要这些东西&#xff0c;至少你可以通过network map绘制一…

.NET 面试题(2)

61、Application 、Cookie和 Session 两种会话有什么不同&#xff1f; 1.Application 储存在服务端&#xff0c;没有时间限制&#xff0c;服务器关闭即销毁&#xff08;前提是自己没写销毁方法&#xff09; 2.Session 储存在服务端&#xff0c;客户端&#xff08;浏览器&#x…

盘点程序员必备的专业术语,值得看一看

英文缩写 API 应用程序接口&#xff08;英语&#xff1a;Application Programming Interface&#xff0c;简称&#xff1a;API&#xff09;&#xff0c;又称为应用编程接口&#xff0c;就是软件系统不同组成部分衔接的约定。由于近年来软件的规模日益庞大&#xff0c;常常需要把…

app上线发布流程_APP上线发布流程

上线流程上线流程的主要步骤在上线之前对程序进行内存动态和静态分析&#xff0c;注意启动图的屏幕适配&#xff0c;不然会自动拉升屏幕&#xff0c;使得屏幕自动适配失效生成cer证书&#xff1a;cer是一个跟电脑相关联的证书文件&#xff0c;让电脑具备真机调试的功能要借助CS…

java.io.IOException: Broken pipe 的异常处理

断开的管道 java.io.IOException: Broken pipe 解决方法 一、Broken pipe产生原因分析 1.当访问某个服务突然服务器挂了&#xff0c;就会产生Broken pipe; 2.客户端读取超时关闭了连接&#xff0c;这时服务器往客户端再写数据就发生了broken pipe异常&#xff01; 二、方案…

IDEA中安装配置Jrebel热部署插件用法笔记

今天给大家介绍一下IDEA中安装配置Jrebel热部署插件用法&#xff0c;希望对大家能有所帮助&#xff01;1、Jrebel插件介绍JRebel属于一款JAVA虚拟机插件&#xff0c;它可以让你的项目在不需要进行重新编译的情况下&#xff0c;就可以实时看到代码的变化对项目的影响。这对我们开…

集成支付宝钱包支付iOS SDK的方法与经验

下载 首先&#xff0c;你要想找到这个SDK&#xff0c;都得费点功夫。现在的SDK改名叫移动支付集成开发包了。 https://b.alipay.com/order/productDetail.htm?productId2013080604609654&tabId4#ps-tabinfo-hash Baidu和Googlep排在前面的支付宝开放平台&#xff0c;里面的…

zabbix 监控项自动发现过滤_【Zabbix监控教程】LLD低级别自动发现配置教程

低级别自动发现简称LLD&#xff0c;使用低级别自动发现可以实现根据Agent的实际情况去自动创建监控项&#xff0c;适用于配置不一的环境。比如A主机有3块硬盘&#xff0c;B主机有5块硬盘&#xff0c;如果不使用LLD的话就需要人肉去为每个硬盘创建监控项。而有了LLD就可以实现自…

Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]

一&#xff1a;运行test类方法时候报错 报错 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify BootstrapWiths value attribute or make the default bootstrapper class available.at org.springframework.test.context.BootstrapU…

PHP底层原理分析和底层扩展编写

运行原理 http://www.phpchina.com/article-40203-1.htmlPHP底层开发 可以理解为就是C的开发&#xff0c;那么简单地说如果我们要查看某个PHP函数的底层实现怎么看呢&#xff1f;需要PHP源码包&#xff1a;http://www.php.net/downloads.php主要目录是Zend 和ext(写扩展的目录…

硬件知识:DP接口和HDMI接口对比,看完你就懂了

目录 一、DP接口 二、HDMI接口 三、总结 电脑显示器高清传输通过会用到两个接口&#xff0c;就是DP接口和HDMI接口&#xff0c;今天电脑学习小编带大家对比一下这两个接口。 一、DP接口 DisplayPort缩写DP&#xff0c;是一个由PC及芯片制造商联盟开发&#xff0c;视频电子标准协…

文治者必有武备不然长大了挨欺负_【博古斋·六月春拍】人文事者必有武备

原标题&#xff1a;【博古斋六月春拍】人文事者必有武备上海博古斋 2017年春季艺术品拍卖会6月24日 中国书画 文房雅玩6月25日 古籍善本文献资料上海大剧院八楼宴会厅(上海市黄陂北路200号A1门入口)图录已寄出&#xff0c;请各位藏友注意查收人文事者必有武备LOT 1649 武备志二…

@Transactional 使用

1 .添加位置 1&#xff09;接口实现类或接口实现方法上&#xff0c;而不是接口类中。 2&#xff09;访问权限&#xff1a;public 的方法才起作用。Transactional 注解应该只被应用到 public 方法上&#xff0c;这是由 Spring AOP 的本质决定的。 系统设计&#xff1a;将标签放…

SpringBoot集成JPA用法笔记

今天给大家整理SpringBoot集成JPA用法。希望对大家能有所帮助&#xff01;搭建SpringBoot项目新建配置文件 application.ymlserver: port: 8090 spring: #通用的数据源配置datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useSSLf…

【转】有的共享软件赚了一百万美元,而为什么你没有?我的软件推广成功之路...

有的共享软件赚了一百万美元&#xff0c;而为什么你没有&#xff1f; 转自&#xff1a;http://blog.csdn.net/wangjiwei2010/article/details/1267044 译&#xff1a;DreamGoal 原作&#xff1a;Steve Pavlin 几个月以前&#xff0c;我决定对共享软件业进行一项非正式的长期研…