编程语言
首页 > 编程语言> > C# datatble 转json数组

C# datatble 转json数组

作者:互联网

#region 将datatable转换成json数组
                System.Collections.ArrayList dic = new System.Collections.ArrayList();
                DataTable dt = null;
                DataTable dtNew = null;
                DataRow[] drArr = null;
                if (dataSet.Tables.Count > 0)
                {
                    dt = dataSet.Tables[0];
                    drArr = dt.Select("");
                    //按条件查询,只能查询当前页,查询所有数据sql语句已经写了
                    dtNew = dt.Clone();
                    for (int i = 0; i < drArr.Length; i++)
                    {
                        dtNew.ImportRow(drArr[i]);
                    }
                }
                foreach (DataRow dr in dtNew.Rows)
                {
                    System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        //drow.Add($"{dc.ColumnName}", dr[dc.ColumnName]);
                        drow.Add(dc.ColumnName, StampToDateTime(Convert.ToString(dr[dc.ColumnName])));
                    }
                    dic.Add(drow);

                }

                #endregion

 

标签:datatble,C#,System,dc,json,Collections,dtNew,dt,drArr
来源: https://www.cnblogs.com/myleave/p/13827413.html