项目中出现数据量特别大的DataTable,做循环处理时比较慢,
需要将DataTable拆分成多个做多线程处理。
将DataTable拆分成多个加到DataSet里,用的时候遍历DataSet就可以了
拆分的方法如下:
/// <summary> /// 分解数据表 /// </summary> /// <param name="orgTable">需要分解的表</param> /// <param name="rowsNum">每个表包含的数据量</param> /// <returns></returns> public static DataSet SplitDataTable(DataTable orgTable, int rowsNum) { var ds = new DataSet(); if (rowsNum <= 0 || orgTable.Rows.Count <= 0) { ds.Tables.Add(orgTable); return ds; } var tableNum = Convert.ToInt32(Math.Ceiling(orgTable.Rows.Count * 1.0 / rowsNum)); var remainder = orgTable.Rows.Count % rowsNum; if (tableNum == 1) { ds.Tables.Add(orgTable); } else { for (var i = 0; i < tableNum; i++) { var table = orgTable.Clone(); int num; if (i != tableNum - 1) num = rowsNum; else num = remainder > 0 ? remainder : rowsNum; for (var j = 0; j < num; j++) { var row = orgTable.Rows[i * rowsNum + j]; table.ImportRow(row); } ds.Tables.Add(table); } } return ds; }