1.根据Dataset生成json格式的字符串,不管Dataset里面有多少个表都可以一一生成对应的json字符串,并一次性返回

private string dsToJson(DataSet ds)
{
System.Text.StringBuilder str = new System.Text.StringBuilder("[");
for (int o = ; o < ds.Tables.Count; o++)
{
str.Append("{");
str.Append(string.Format("\"{0}\":[", ds.Tables[o].TableName)); for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
str.Append("{");
for (int j = ; j < ds.Tables[].Columns.Count; j++)
{
str.Append(string.Format("\"{0}\":\"{1}\",", ds.Tables[].Columns[j].ColumnName, ds.Tables[].Rows[i][j].ToString()));
}
str.Remove(str.Length - , );
str.Append("},");
}
str.Remove(str.Length - , );
str.Append("]},");
}
str.Remove(str.Length - , );
str.Append("]");
return str.ToString();
}

2.数据库中的内容转换成json数据

  [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
private string dsToJson()
{
System.Text.StringBuilder str = new System.Text.StringBuilder("[");
for (int o = ; o < ds.Tables.Count; o++)
{
str.Append("{");
str.Append(string.Format("\"{0}\":[", ds.Tables[o].TableName)); for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
str.Append("{");
for (int j = ; j < ds.Tables[].Columns.Count; j++)
{
str.Append(string.Format("\"{0}\":\"{1}\",", ds.Tables[].Columns[j].ColumnName, ds.Tables[].Rows[i][j].ToString()));
}
str.Remove(str.Length - , );
str.Append("},");
}
str.Remove(str.Length - , );
str.Append("]},");
}
str.Remove(str.Length - , );
str.Append("]"); return str.ToString();
}

3.C# - list<>数据填充到Dataset里

publicstatic DataSet ConvertToDataSet<T>(IList<T> list)
{
if (list ==null|| list.Count <=)
{
returnnull;
}
DataSet ds =new DataSet();
DataTable dt =new DataTable(typeof(T).Name);
DataColumn column;
DataRow row; System.Reflection.PropertyInfo[] myPropertyInfo =typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t ==null)
{
continue;
}
row = dt.NewRow();
for (int i =, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] ==null)
{
column =new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
}

4.DataSet 和 Datable 的相互转化

DataSet ds = DbHelperSQL.Query("select * from text_table where a='3'");
string strtext = ds.Tables[].Rows[]["bz"].ToString();

5.List转化成数组

List<string> a = new List<string>();
public string[] getString(TreeNodeCollection item)
{
NumMethod(item);
return a.ToArray();
}

6.将字符转转化成数组并以逗号分割,即,去掉逗号

string strszcdid = "1111,111111,111112,111211,111311,";

string[] a = strszcdid.Split(',');

7.截取字符转从字符串指定位置截取到固定的长度

id = id.Substring(, id.Length - );

DataSet、DataTable、Json、List 等各种数据的相互转化的更多相关文章

  1. Asp.net 将DataTable 或者DataSet 转换为Json 格式

    Web 开发中,将从数据库中取到的数据直接转换为 Json 格式的数据,在前台通过Ajax 无刷新显示在界面上,下面提供将DataTable 或者DataSet 转换为Json 的方法 /// < ...

  2. dataset datatable 转json

    class ToJosn { #region dataTable转换成Json格式 /// <summary> /// dataTable转换成Json格式 /// </summar ...

  3. WCF分布式开发步步为赢(8):使用数据集(DataSet)、数据表(DataTable)、集合(Collection)传递数据

    数据集(DataSet).数据表(DataTable).集合(Collection)概念是.NET FrameWork里提供数据类型,在应用程序编程过程中会经常使用其来作为数据的载体,属于ADO.NE ...

  4. [Json] C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json (转载)

    点击下载 ConvertJson.rar 本类实现了 C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json|等功能大家先预 ...

  5. LINQ返回DataTable类型 list转dataset 转换为JSON对象

    using System.Web.Script.Serialization; using System.Collections.Generic; using System.Reflection; us ...

  6. C#使用DataSet Datatable更新数据库的三种实现方法

    本文以实例形式讲述了使用DataSet Datatable更新数据库的三种实现方法,包括CommandBuilder 方法.DataAdapter 更新数据源以及使用sql语句更新.分享给大家供大家参 ...

  7. DataSet转Json 方法

    防止忘记,留着备用 /// <summary> /// DATASET转JSON /// </summary> /// <param name="ds" ...

  8. c#实现list,dataset,DataTable转换成josn等各种转换方法总和

    using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Refle ...

  9. 在Bootstrap开发框架中使用dataTable直接录入表格行数据(2)--- 控件数据源绑定

    在前面随笔<在Bootstrap开发框架中使用dataTable直接录入表格行数据>中介绍了在Web页面中使用Jquery DataTable插件进行对数据直接录入操作,这种处理能够给用户 ...

随机推荐

  1. iOS - YYAdd对UIDevice的拓展

    YYKit中对UIDevice的拓展,accumulate knowledge !!! 首先 #include <sys/socket.h> #include <sys/sysctl ...

  2. javascript判断手机浏览器版本信息

    <script type="text/javascript"> /* * 智能机浏览器版本信息: * */ var browser={ versions:functio ...

  3. loopback文档翻译

    最近在学习loopback,期间在strongloop的官网翻译了部分文章. 见:https://docs.strongloop.com/pages/viewpage.action?pageId=60 ...

  4. PhpStorm 9.03 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项

    第一步:配置 git.exe File -> Default Settings -> Version Control -> Git -> Path go Git executa ...

  5. 2.4使用属性在 ASP.NET Web API 2 路由创建一个 REST API

    Web API 2 支持一种新型的路由,称为属性路由.属性路由的一般概述,请参阅属性路由 Web API 2 中.在本教程中,您将使用属性路由创建一个 REST API 集合的书.API 将支持以下操 ...

  6. 【转载】实时监听输入框值变化的完美方案:oninput & onpropertychange

    oninput 是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有 ...

  7. 二叉树建立,遍历和二叉排序树的判断【c++】

    // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  8. phpcms

    phpcms 织梦 帝国cms

  9. HttpClient, HttpClientHandler, and WebRequestHandler Explained

    原文地址 https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequest ...

  10. Angular2 指令

      1. 指令说明 Angular2 指令是构成Angular2应用程序的重要组成部分,指令主要用来对模板的标签或者元素附加一些新的特性或者功能,改变一个 DOM 元素的外观或行为,Angular2指 ...