博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Datatable/Dataset 转 JSON方法
阅读量:4308 次
发布时间:2019-06-06

本文共 5304 字,大约阅读时间需要 17 分钟。

当数据库表的数据在一般处理程序中查出来需要将这个表数据返回到前台的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;/// ///DataHelper 的摘要说明/// public class DataHelper{    public DataHelper()    {        //        //TODO: 在此处添加构造函数逻辑        //    }    #region DataTable转Json    ///     /// DataTable转Json for DataGrid    ///     ///     /// 
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 /// /// 将Datatable 中得数据行进行分页,并将页内数据返回 /// /// /// 行数 /// 第几页 ///
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 /// /// DataTable转Json for DataGrid /// /// ///
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 /// /// DataTable转Json for DataGrid /// /// ///
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格式 /// /// dataTable转换成Json格式 /// /// ///
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

你可能感兴趣的文章
我们一起做一个可以商用的springboot脚手架
查看>>
idea在搭建ssm框架时mybatis整合问题 无法找到mapper
查看>>
java设计基本原则----单一职责原则
查看>>
HashMap的实现
查看>>
互斥锁 synchronized分析
查看>>
java等待-通知机制 synchronized和waity()的使用实践
查看>>
win10 Docke安装mysql8.0
查看>>
docker 启动已经停止的容器
查看>>
order by 排序原理及性能优化
查看>>
Lock重入锁
查看>>
docker安装 rabbitMq
查看>>
git 常用命令 入门
查看>>
关闭selinx nginx无法使用代理
查看>>
shell 脚本部署项目
查看>>
spring cloud zuul网关上传大文件
查看>>
springboot+mybatis日志显示SQL
查看>>
工作流中文乱码问题解决
查看>>
maven打包本地依赖包
查看>>
spring boot jpa 实现拦截器
查看>>
jenkins + maven+ gitlab 自动化部署
查看>>