码迷,mamicode.com
首页 > Web开发 > 详细

asp.net 根据dataset获取大字符串(直接转出json)

时间:2015-09-17 19:11:22      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

方法:

public static string GetJsonByDataset(DataSet ds)        

{         

   if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)       

      {              

     return null;          

  }            

  StringBuilder sb = new StringBuilder();          

    foreach (DataTable dt in ds.Tables)            

  {   

             sb.Append(string.Format("["));             

       foreach (DataRow dr in dt.Rows)             

       {                    

      sb.Append("{");                    

      for (int i = 0; i < dr.Table.Columns.Count; i++)                    

      {                        

        sb.AppendFormat("\"{0}\":\"{1}\",", dr.Table.Columns[i].ColumnName, dr[i].ToString());  

                  }                   

        sb.Remove(sb.ToString().LastIndexOf(‘,‘), 1);           

             sb.Append("},");              

       }

               sb.Remove(sb.ToString().LastIndexOf(‘,‘), 1);         

           sb.Append("]");

         }            

   return sb.ToString();      

  }

asp.net 根据dataset获取大字符串(直接转出json)

标签:

原文地址:http://www.cnblogs.com/jobyym/p/4816978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!