C# 将一个DataTable分解成多个DataTable
标签:nbsp sky details ++ for pre get last else
今天在做项目时需要将一个DataTable分解成多个DataTable分批传入函数里面,于是在网上找寻了一番,修复了angle_greensky110 存在分表的缺陷的代码。
分表方法:
///
/// 分解数据表
///
/// 需要分解的表
/// 每个表包含的数据量
///
public static DataSet SplitDataTable(DataTable originalTab, int rowsNum)
{
int tableNum = System.Convert.ToInt32(originalTab.Rows.Count / rowsNum); //相除取整
int remainder = System.Convert.ToInt32(originalTab.Rows.Count % rowsNum); //相除取余数
DataSet ds = new DataSet();
//if one table is big enough to store, use one table
if (tableNum == 0)
{
ds.Tables.Add(originalTab);
}
else
{
if (remainder > 0) //如果有余数,需要多一张表存余数
{
tableNum++;
}
DataTable[] tableSlice = new DataTable[tableNum - 1 + 1];
//Save orginal columns into new table
int c = 0;
for (c = 0; c 1); c++)
{
tableSlice[c] = new DataTable();
foreach (DataColumn dc in originalTab.Columns)
{
tableSlice[c].Columns.Add(dc.ColumnName, dc.DataType);
}
}
//Import Rows
int i = 0;
if (remainder > 0)
{
for (i = 0; i 1); i++)
{
//if the current table is not the last table
if (i != tableNum - 1)
{
int j = 0;
for (j = i * rowsNum; j 1) * rowsNum) - 1); j++)
{
tableSlice[i].ImportRow(originalTab.Rows[j]);
}
}
else
{
int k = 0;
//For k = i * rowsNum To (((i + 1) * rowsNum + remainder) - 1)
for (k = i * rowsNum; k 1); k++)
{
tableSlice[i].ImportRow(originalTab.Rows[k]);
}
}
}
}
else
{
for (i = 0; i 1); i++)
{
int j = 0;
for (j = i * rowsNum; j 1) * rowsNum) - 1); j++)
{
tableSlice[i].ImportRow(originalTab.Rows[j]);
}
}
}
//Add all tables into a dataset
foreach (DataTable dt in tableSlice)
{
ds.Tables.Add(dt);
}
}
return ds;
}
C# 将一个DataTable分解成多个DataTable
标签:nbsp sky details ++ for pre get last else
原文地址:https://www.cnblogs.com/wofeiliangren/p/13157701.html
评论