1 #region 數據表DataTable 轉鍵值對集合 List 2 /// <summary> 3 /// 數據表DataTable 轉鍵值對集合 List 4 /// 把DataTable轉成 List集合, 存每一行 5 /// 集合中放的是鍵值對字典,存每一列 6 /// </summary> 7 /// <param name="dt">數據表</param> 8 /// <returns>哈希表數組</returns> 9 public static List<Dictionary<string, object>> DataTableToList(DataTable dt) 10 { 11 List<Dictionary<string, object>> list 12 = new List<Dictionary<string, object>>(); 13 14 foreach (DataRow dr in dt.Rows) 15 { 16 Dictionary<string, object> dic = new Dictionary<string, object>(); 17 foreach (DataColumn dc in dt.Columns) 18 { 19 dic.Add(dc.ColumnName, dr[dc.ColumnName]); 20 } 21 list.Add(dic); 22 } 23 return list; 24 } 25 #endregion 26 27 #region 數據集轉鍵值對數組字典 28 29 /// <summary> 30 /// 數據集轉鍵值對數組字典 31 /// </summary> 32 /// <param name="dataSet">數據集</param> 33 /// <returns>鍵值對數組字典</returns> 34 public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds) 35 { 36 Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>(); 37 38 foreach (DataTable dt in ds.Tables) 39 result.Add(dt.TableName, DataTableToList(dt)); 40 41 return result; 42 } 43 #endregion 44 45 #region 數據表Tables 轉JSON 46 /// <summary> 47 /// 數據表轉Tables JSON 48 /// </summary> 49 /// <param name="dataTable">數據表</param> 50 /// <returns>JSON字符串</returns> 51 public static string DataTableToJSON(DataTable dt) 52 { 53 return JsonHelper.ObjectToJSON(DataTableToList(dt)); 54 } 55 #endregion 56 57 #region 將datatable轉換為json 58 /// <summary> 59 /// 將datatable轉換為json 60 /// </summary> 61 /// <param name="dtb">Dt</param> 62 /// <returns>JSON字符串</returns> 63 public static string Dtb2Json(DataTable dtb) 64 { 65 //JavaScriptSerializer jss = new JavaScriptSerializer(); 66 67 System.Collections.ArrayList dic = new System.Collections.ArrayList(); 68 69 foreach (DataRow dr in dtb.Rows) 70 { 71 System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>(); 72 73 foreach (DataColumn dc in dtb.Columns) 74 { 75 drow.Add(dc.ColumnName, dr[dc.ColumnName]); 76 } 77 78 dic.Add(drow); 79 } 80 81 return null; 82 } 83 #endregion 84 85 #region 將Dictionary轉換為數據表數據 Tables 86 public static DataTable DictToDataTable(Dictionary<string, object> dict) 87 { 88 DataTable dt = new DataTable(); 89 90 //dt.Columns.Add("ID", typeof(Guid)); 91 //dt.Columns.Add("DID", typeof(string)); 92 //dt.Columns.Add("DEPARTMENTNUM", typeof(string)); 93 //dt.Columns.Add("DEPARTMENTNAME", typeof(string)); 94 //dt.Columns.Add("REMARKS", typeof(string)); 95 96 foreach (var colName in dict.Keys) 97 { 98 dt.Columns.Add(colName, typeof(string)); 99 } 100 DataRow dr = dt.NewRow(); 101 foreach (KeyValuePair<string, object> item in dict) 102 { 103 dr[item.Key] = item.Value; 104 } 105 dt.Rows.Add(dr); 106 return dt; 107 } 108 #endregion 109 110 #region 將List轉換為數據表數據 Tables 111 /// <summary> 112 /// List轉DataTable 113 /// </summary> 114 public static DataTable ListToDataTable<T>(List<T> list) 115 { 116 if (list == null || list.Count == 0) 117 { 118 return new DataTable(); 119 } 120 121 //獲取T下所有的屬性 122 Type entityType = list[0].GetType(); 123 PropertyInfo[] entityProperties = entityType.GetProperties(); 124 125 DataTable dt = new DataTable("data"); 126 127 for (int i = 0; i < entityProperties.Length; i++) 128 { 129 dt.Columns.Add(entityProperties[i].Name); 130 } 131 132 foreach (var item in list) 133 { 134 if (item.GetType() != entityType) 135 { 136 throw new Exception("要轉換集合元素類型不一致!"); 137 } 138 //創建一個用於放所有屬性值的數組 139 object[] entityValues = new object[entityProperties.Length]; 140 for (int i = 0; i < entityProperties.Length; i++) 141 { 142 entityValues[i] = entityProperties[i].GetValue(item, null); 143 } 144 145 dt.Rows.Add(entityValues); 146 } 147 return dt; 148 } 149 #endregion 150 151 #region Json 字符串 轉換為 DataTable數據集合 簡要版,正在使用中 152 /// <summary> 153 /// Json 字符串 轉換為 DataTable數據集合 簡要版,正在使用中 154 /// </summary> 155 /// <param name="json"></param> 156 /// <returns></returns> 157 /// 158 //格式; 159 //[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}] 160 public static DataTable ToDataTableTwo(string json) 161 { 162 DataTable dataTable = new DataTable(); //實例化 163 DataTable result; 164 try 165 { 166 List<Dictionary<string, object>> arrayList = JsonHelper.JSONToObject<List<Dictionary<string, object>>>(json); 167 168 if (arrayList != null && arrayList.Count > 0) 169 { 170 foreach (Dictionary<string, object> dictionary in arrayList) 171 { 172 if (dictionary.Keys.Count == 0) 173 { 174 result = dataTable; 175 return result; 176 } 177 178 //Columns 179 if (dataTable.Columns.Count == 0) 180 { 181 foreach (var current in dictionary) 182 { 183 if (current.Value != null) 184 { 185 dataTable.Columns.Add(current.Key, current.Value.GetType()); 186 } 187 else 188 { 189 dataTable.Columns.Add(current.Key); 190 } 191 } 192 } 193 194 //Rows 195 DataRow dataRow = dataTable.NewRow(); 196 197 foreach (string current in dictionary.Keys) 198 { 199 if (dictionary[current] != null) 200 { 201 dataRow[current] = dictionary[current]; 202 } 203 } 204 205 dataTable.Rows.Add(dataRow); //循環添加行到DataTable中 206 } 207 } 208 } 209 catch (Exception ex) 210 { 211 throw ex; 212 } 213 214 result = dataTable; 215 return result; 216 } 217 #endregion