DataSetToJson 扩展方法
001 |
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Linq; |
004 |
using System.Text; |
005 |
using System.Data; |
006 |
|
007 |
namespace Common |
008 |
|
009 |
|
010 |
public static class JsonExtensions |
011 |
|
012 |
#region DataSetToJson 扩展方法 |
013 |
/// <summary> |
014 |
/// DataSetToJson 扩展方法 |
015 |
/// </summary> |
016 |
/// <param name="ds">要传入的DataSet</param> |
017 |
/// <param name="JsonName">Json的名称</param> |
018 |
/// <param name="ParName">Json字段名称</param> |
019 |
/// <returns>返回JSON字符串</returns> |
020 |
public static string DataSetToJson(this DataSet ds, string JsonName, string[] ParName) |
021 |
|
022 |
try |
023 |
|
024 |
if (ds == null |
025 |
|
026 |
return "DataSet Is Null ,So I Can't Do It To Json!" |
027 |
|
028 |
if (JsonName.Length < 1) |
029 |
|
030 |
return "You Set The Json Name Is Wrong!" |
031 |
|
032 |
if (ds.Tables[0].Columns.Count < ParName.Length) |
033 |
|
034 |
return "You Give The ParName Is Bigger Than DataSet Columns!" |
035 |
|
036 |
string josn = "{" + JsonName + ":[" |
037 |
string temp = "" |
038 |
for (int j = 0; j < ds.Tables[0].Rows.Count; j++) |
039 |
|
040 |
temp = temp + "{" |
041 |
for (int i = 0; i < ParName.Length; i++) |
042 |
|
043 |
temp += "" + ParName[i] + ":\'" + ds.Tables[0].Rows[j][ParName[i]] + "\'" |
044 |
if (i != ParName.Length - 1) |
045 |
|
046 |
temp = temp + "," |
047 |
|
048 |
|
049 |
if (j == ds.Tables[0].Rows.Count - 1) |
050 |
|
051 |
temp = temp + "}" |
052 |
|
053 |
else |
054 |
|
055 |
temp = temp + "}," |
056 |
|
057 |
|
058 |
josn = josn + temp + "]}" |
059 |
return josn; |
060 |
|
061 |
catch (Exception ex) |
062 |
|
063 |
return "Codeing is Error----" + ex.ToString(); |
064 |
|
065 |
|
066 |
|
067 |
|
068 |
#endregion |
069 |
|
070 |
#region DataSetToJson 扩展方法 |
071 |
/// <summary> |
072 |
/// DataSetToJson 扩展方法 |
073 |
/// </summary> |
074 |
/// <param name="ds">要传入的DataSet</param> |
075 |
/// <returns>返回JSON字符串<</returns> |
076 |
public static string DataSetToJson(this DataSet ds) |
077 |
|
078 |
try |
079 |
|
080 |
if (ds == null |
081 |
|
082 |
return "DataSet Is Null ,So I Can't Do It To Json!" |
083 |
|
084 |
string josn = "[" |
085 |
string temp = "" |
086 |
for (int j = 0; j < ds.Tables[0].Rows.Count; j++) |
087 |
|
088 |
temp = temp + "{" |
089 |
for (int i = 0; i < ds.Tables[0].Columns.Count; i++) |
090 |
|
091 |
temp += "" + ds.Tables[0].Columns[i].ColumnName + ":\'" + ds.Tables[0].Rows[j][i] + "\'" |
092 |
if (i != ds.Tables[0].Columns.Count - 1) |
093 |
|
094 |
temp = temp + "," |
095 |
|
096 |
|
097 |
if (j == ds.Tables[0].Rows.Count - 1) |
098 |
|
099 |
temp = temp + "}" |
100 |
|
101 |
else |
102 |
|
103 |
temp = temp + "}," |
104 |
|
105 |
|
106 |
josn = josn + temp + "]" |
107 |
return josn; |
108 |
|
109 |
catch (Exception ex) |
110 |
|
111 |
return "Codeing is Error----" + ex.ToString(); |
112 |
|
113 |
|
114 |
|
115 |
#endregion |
116 |
|
117 |
} |
DataSetToJson 扩展方法的更多相关文章
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- 扩展方法(C#)
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 下面的示例为String添加 ...
- 扩展方法解决LinqToSql Contains超过2100行报错问题
1.扩展方法 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- C#扩展方法
扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法就相当于一个马甲,给一个现有类套上,就可以为这个类添加其他方法了. 马甲必须定义为stati ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
- 扩展方法 1 简单的string扩展方法
这里是关于 String的简单扩展方法 (静态类 静态方法 this 类型 这里是string) static class Program { static void Main(string[] ar ...
随机推荐
- 【JAVA零基础入门系列】Day6 Java字符串
字符串,是我们最常用的类型,每个用双引号来表示的串都是一个字符串.Java中的字符串是一个预定义的类,跟C++ 一样叫String,而不是Char数组.至于什么叫做类,暂时不做过多介绍,在之后的篇章中 ...
- 1-git初体验
1 准备工作: windows系统下,安装好msysgit -安装好后,在开始菜单找到Git > Git bash 2 当前电脑配置用户名 邮箱 $ git config --global ...
- ConvertUtils.register注册转换器
当用到BeanUtils的populate.copyProperties方法或者getProperty,setProperty方法其实都会调用convert进行转换 但Converter只支持一些基本 ...
- MySQL(十四)之数据备份与还原
前言 上一篇分享了关于MySQL事务的知识,在我们数据库中最重要的就是数据了,所以数据的备份就显的特别的重要! 为什么要备份数据? 在生产环境中我们数据库可能会遭遇各种各样的不测从而导致数据丢失, 大 ...
- 从零开始搭建框架SSM+Redis+Mysql(一)之摘要
从零开始搭建框架SSM+Redis+Mysql(一)之摘要 本文章为本人实际的操作后的回忆笔记,如果有步骤错漏,希望来信307793969@qq.com或者评论指出. 本文章只体现过程,仅体现操作流程 ...
- sql server 2008 18456错误
来自:http://blog.csdn.net/qishuangquan/article/details/6024767 百度搜18456错误几乎只能搜到一篇文章,并不是说结果条数,而是所有的文章都是 ...
- win10 uwp 绘图 Line 控件使用
本文主要讲一个在绘图中,我们会有一个基础的控件,Line.控件的基本使用和他能做出的我们很多时候需要的界面. 虽然是一个简单控件,但是可以做出很诡异的很好看的UI. 首先,我们要知道,Line就是画直 ...
- .NET Framework基本概念
http://blog.csdn.net/T573029173/article/details/41730101 .NET是微软的新一代技术平台.对技术人员来说,想真正了解什么是.NET,须先了解.N ...
- Tomcat Java.OutOfMemoryError : PermGen Space异常
背景:前些日子更新公司多年前一个旧平台发布到Tomcat上之后,频繁收到网站许多模块无法正常使用的反汇. 测试过程中发现平台发布一段时间后,访问相关网页出现如下500页面 解决方案:PermGen s ...
- 【Spring】构建Spring Web应用
前言 学习了Spring的注解.AOP后,接着学习Spring Web,对于Web应用开发,Spring提供了Web框架. Web应用 Spring MVC初探 MVC为(Model-View-Con ...