C# 代码:
///
<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;
}
/// 分解数据表
/// </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;
}
VB.NET 代码:
'
===============================================================================
' Author: Ray Chang
' Date: 2007/04/11
' Description: This function splits a givin datatabe into several tables and
' create a new dataset to hold these tables.
' ================================================================================
Public Function SplitDataTable( ByVal originalTab As DataTable, ByVal rowsNum As Integer ) As DataSet
Dim tableNum As Integer = originalTab.Rows.Count \ rowsNum
Dim remainder As Integer = originalTab.Rows.Count Mod rowsNum
Dim ds As DataSet = New DataSet
' if one table is big enough to store, use one table
If tableNum = 0 Then
ds.Tables.Add(originalTab)
Else
Dim tableSlice(tableNum - 1 ) As DataTable
' Save orginal columns into new table
Dim c As Integer
For c = 0 To (tableNum - 1 )
tableSlice(c) = New DataTable
For Each dc As DataColumn In originalTab.Columns
tableSlice(c).Columns.Add(dc.ColumnName, dc.DataType)
Next
Next
' Import Rows
Dim i As Integer
For i = 0 To (tableNum - 1 )
' if the current table is not the last table
If i <> tableNum - 1 Then
Dim j As Integer
For j = i * rowsNum To (((i + 1 ) * rowsNum) - 1 )
tableSlice(i).ImportRow(originalTab.Rows(j))
Next
Else
Dim k As Integer
For k = i * rowsNum To (((i + 1 ) * rowsNum + remainder) - 1 )
tableSlice(i).ImportRow(originalTab.Rows(k))
Next
End If
Next
' Add all tables into a dataset
For Each dt As DataTable In tableSlice
ds.Tables.Add(dt)
Next
End If
' return dataset
SplitDataTable = ds
End Function
' Author: Ray Chang
' Date: 2007/04/11
' Description: This function splits a givin datatabe into several tables and
' create a new dataset to hold these tables.
' ================================================================================
Public Function SplitDataTable( ByVal originalTab As DataTable, ByVal rowsNum As Integer ) As DataSet
Dim tableNum As Integer = originalTab.Rows.Count \ rowsNum
Dim remainder As Integer = originalTab.Rows.Count Mod rowsNum
Dim ds As DataSet = New DataSet
' if one table is big enough to store, use one table
If tableNum = 0 Then
ds.Tables.Add(originalTab)
Else
Dim tableSlice(tableNum - 1 ) As DataTable
' Save orginal columns into new table
Dim c As Integer
For c = 0 To (tableNum - 1 )
tableSlice(c) = New DataTable
For Each dc As DataColumn In originalTab.Columns
tableSlice(c).Columns.Add(dc.ColumnName, dc.DataType)
Next
Next
' Import Rows
Dim i As Integer
For i = 0 To (tableNum - 1 )
' if the current table is not the last table
If i <> tableNum - 1 Then
Dim j As Integer
For j = i * rowsNum To (((i + 1 ) * rowsNum) - 1 )
tableSlice(i).ImportRow(originalTab.Rows(j))
Next
Else
Dim k As Integer
For k = i * rowsNum To (((i + 1 ) * rowsNum + remainder) - 1 )
tableSlice(i).ImportRow(originalTab.Rows(k))
Next
End If
Next
' Add all tables into a dataset
For Each dt As DataTable In tableSlice
ds.Tables.Add(dt)
Next
End If
' return dataset
SplitDataTable = ds
End Function
先用C#写的,可是系统用的却是VB.NET,只能又转成了VB.NET. 整个方法返回一个包含了分解后的table的DataSet, 所传入的参数主要2个:一是所需要分解的表,还有一个是每一个表所包含的数据量,而最后一个表将会包含设定的数据量加上余数。至于转换后如何操作,我就不罗嗦了。
这个方法应用的地方不多,除非特别案例,如果那个系统从开头设计好,估计后期也不用做这种改动。总之贴出来,能用到大家就用吧。
- Ray Chang 2007年4月11日
转自: http://www.cnblogs.com/Rayinuk/archive/2008/11/04/709336.html