/// <summary> /// 分解数据表 /// </summary> /// <param name="originalTab">需要分解的表</param> /// <param name="rowsNum">每个表包含的数据量</param> /// <returns></returns> public DataSet SplitDataTable(DataTable originalTab, int rowsNum) { //获取所需创建的表数量 int tableNum = originalTab.Rows.Count / rowsNum; //获取数据余数 int remainder = originalTab.Rows.Count % rowsNum; DataSet ds = new DataSet(); //如果只需要创建1个表,直接将原始表存入DataSet if (tableNum == 0) { ds.Tables.Add(originalTab); } else { DataTable[] tableSlice = new DataTable[tableNum]; //Save orginal columns into new table. for (int c = 0; c < tableNum; c++) { tableSlice[c] = new DataTable(); foreach (DataColumn dc in originalTab.Columns) { tableSlice[c].Columns.Add(dc.ColumnName, dc.DataType); } } //Import Rows for (int i = 0; i < tableNum; i++) { // if the current table is not the last one if (i != tableNum - 1) { for (int j = i * rowsNum; j < ((i + 1) * rowsNum); j++) { tableSlice[i].ImportRow(originalTab.Rows[j]); } } else { for (int k = i * rowsNum; k < ((i + 1) * rowsNum + remainder); k++) { tableSlice[i].ImportRow(originalTab.Rows[k]); } } } //add all tables into a dataset foreach (DataTable dt in tableSlice) { ds.Tables.Add(dt); } } return ds;
另外,注意程序使用64位编译。
原文链接: https://www.cnblogs.com/wodegui/p/5564872.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/234691
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!