小荷才露尖尖角

导航

DataSetToJson 扩展方法

 

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

namespace Common
{

    public static class JsonExtensions
    {
        #region DataSetToJson 扩展方法
        /// <summary>
        /// DataSetToJson 扩展方法
        /// </summary>
        /// <param name="ds">要传入的DataSet</param>
        /// <param name="JsonName">Json的名称</param>
        /// <param name="ParName">Json字段名称</param>
        /// <returns>返回JSON字符串</returns>
        public static string DataSetToJson(this DataSet ds, string JsonName, string[] ParName)
        {
            try
            {
                if (ds == null)
                {
                    return "DataSet Is Null ,So I Can't Do It To Json!";
                }
                if (JsonName.Length < 1)
                {
                    return "You Set The Json Name Is Wrong!";
                }
                if (ds.Tables[0].Columns.Count < ParName.Length)
                {
                    return "You Give The ParName Is Bigger Than DataSet Columns!";
                }
                string josn = "{" + JsonName + ":[";
                string temp = "";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    temp = temp + "{";
                    for (int i = 0; i < ParName.Length; i++)
                    {
                        temp += "" + ParName[i] + ":\'" + ds.Tables[0].Rows[j][ParName[i]] + "\'";
                        if (i != ParName.Length - 1)
                        {
                            temp = temp + ",";
                        }
                    }
                    if (j == ds.Tables[0].Rows.Count - 1)
                    {
                        temp = temp + "}";
                    }
                    else
                    {
                        temp = temp + "},";
                    }
                }
                josn = josn + temp + "]}";
                return josn;
            }
            catch (Exception ex)
            {
                return "Codeing is Error----" + ex.ToString();
            }

        }

        #endregion

        #region  DataSetToJson 扩展方法
        /// <summary>
        /// DataSetToJson 扩展方法
        /// </summary>
        /// <param name="ds">要传入的DataSet</param>
        /// <returns>返回JSON字符串<</returns>
        public static string DataSetToJson(this DataSet ds)
        {
            try
            {
                if (ds == null)
                {
                    return "DataSet Is Null ,So I Can't Do It To Json!";
                }
                string josn = "[";
                string temp = "";
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    temp = temp + "{";
                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                    {
                        temp += "" + ds.Tables[0].Columns[i].ColumnName + ":\'" + ds.Tables[0].Rows[j][i] + "\'";
                        if (i != ds.Tables[0].Columns.Count - 1)
                        {
                            temp = temp + ",";
                        }
                    }
                    if (j == ds.Tables[0].Rows.Count - 1)
                    {
                        temp = temp + "}";
                    }
                    else
                    {
                        temp = temp + "},";
                    }
                }
                josn = josn + temp + "]";
                return josn;
            }
            catch (Exception ex)
            {
                return "Codeing is Error----" + ex.ToString();
            }

        }
        #endregion
    }
}

 

 

posted on 2010-05-12 08:20  小.荷  阅读(851)  评论(2编辑  收藏  举报