1.將泛型集合類轉(zhuǎn)換成DataTable(表中無(wú)數(shù)據(jù)時(shí)使用):
十年的譙城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整譙城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“譙城網(wǎng)站設(shè)計(jì)”,“譙城網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
public static DataTable NullListToDataTable(IList list) { var result = new DataTable(); if (list.Count <= 0) return result; var propertys = list[0].GetType().GetProperties(); foreach (var pi in propertys) { if (pi != null) { result.Columns.Add(pi.Name, pi.PropertyType); } } for (var i = 0; i < list.Count; i++) { var tempList = new ArrayList(); foreach (var pi in propertys) { var obj = pi.GetValue(list[i], null); tempList.Add(obj); } var array = tempList.ToArray(); result.LoadDataRow(array, true); } return result; }
2.將泛型集合類轉(zhuǎn)換成DataTable(表中有數(shù)據(jù)時(shí)使用):
public static DataTable NoNullListToDataTable<T>(IList<T> list) { var ds = new DataSet(); var dt = new DataTable(typeof(T).Name); var myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var t in list) { if (t == null) continue; var row = dt.NewRow(); for (int i = 0, j = myPropertyInfo.Length; i < j; i++) { var pi = myPropertyInfo[i]; var name = pi.Name; if (dt.Columns[name] != null) continue; DataColumn column; if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]") { column = new DataColumn(name, typeof(int)); dt.Columns.Add(column); if (pi.GetValue(t, null) != null) row[name] = pi.GetValue(t, null); else row[name] = DBNull.Value; } else { 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.Tables[0]; }
3.表中有數(shù)據(jù)或無(wú)數(shù)據(jù)時(shí)使用,可排除DATASET不支持System.Nullable錯(cuò)誤:
public static DataTable ToDataTable<T>(IList<T> list) { if (list == null || list.Count <= 0) { var result = new DataTable(); object temp; if (list == null || list.Count <= 0) return result; var propertys = list[0].GetType().GetProperties(); foreach (var pi in propertys) { { result.Columns.Add(pi.Name, pi.PropertyType); } } for (var i = 0; i < list.Count; i++) { var tempList = new ArrayList(); foreach (var pi in propertys) { var obj = pi.GetValue(list[i], null); tempList.Add(obj); } var array = tempList.ToArray(); result.LoadDataRow(array, true); } return result; } var ds = new DataSet(); var dt = new DataTable(typeof(T).Name); var myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var t in list) { if (t == null) continue; var row = dt.NewRow(); for (int i = 0, j = myPropertyInfo.Length; i < j; i++) { var pi = myPropertyInfo[i]; var name = pi.Name; if (dt.Columns[name] != null) continue; DataColumn column; if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]") { column = new DataColumn(name, typeof(int)); dt.Columns.Add(column); if (pi.GetValue(t, null) != null) row[name] = pi.GetValue(t, null); else row[name] = DBNull.Value; } else { 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.Tables[0]; }
4.合并相同的DataTable:
public static DataTable MergeSameDatatable(DataTable dataTable1, DataTable dataTable2) { var newDataTable = dataTable1.Clone(); var obj = new object[newDataTable.Columns.Count]; for (var i = 0; i < dataTable1.Rows.Count; i++) { dataTable1.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); } for (var i = 0; i < dataTable2.Rows.Count; i++) { dataTable2.Rows[i].ItemArray.CopyTo(obj, 0); newDataTable.Rows.Add(obj); } return new DataTable(); }
5.將兩個(gè)列不同的DataTable合并成一個(gè)新的DataTable :
public static DataTable UniteDataTable(DataTable dt1, DataTable dt2, string dtName) { var dt3 = dt1.Clone(); for (var i = 0; i < dt2.Columns.Count; i++) { dt3.Columns.Add(dt2.Columns[i].ColumnName); } var obj = new object[dt3.Columns.Count]; for (int i = 0; i < dt1.Rows.Count; i++) { dt1.Rows[i].ItemArray.CopyTo(obj, 0); dt3.Rows.Add(obj); } if (dt1.Rows.Count >= dt2.Rows.Count) { for (var i = 0; i < dt2.Rows.Count; i++) { for (var j = 0; j < dt2.Columns.Count; j++) { dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString(); } } } else { for (var i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++) { var dr3 = dt3.NewRow(); dt3.Rows.Add(dr3); } for (var i = 0; i < dt2.Rows.Count; i++) { for (var j = 0; j < dt2.Columns.Count; j++) { dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString(); } } } dt3.TableName = dtName; return dt3; }
6.Datatable 轉(zhuǎn) List<Dictionary<string, object>:
public static List<Dictionary<string, object>> DataTableToListDictory(DataTable table) { var ld = new List<Dictionary<string, object>>(); for (var i = 0; i < table.Rows.Count; i++) { var dic = new Dictionary<string, object>(); for (var j = 0; j < table.Columns.Count; j++) { dic.Add(table.Columns[j].ColumnName, table.Rows[i][j]); } ld.Add(dic); } return ld; }
網(wǎng)站標(biāo)題:DotNet中的DataTable相關(guān)操作
網(wǎng)址分享:http://aaarwkj.com/article24/gjdece.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗(yàn)、商城網(wǎng)站、電子商務(wù)、網(wǎng)站收錄、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)