将DataTable拆分为多个DataTable

时间:2012-05-23 18:37:40

标签: c# winforms datatable

全部,我有一个实用程序可以将DataTables导出到Excel( .xls和.xlsx文件类型)。当我在DataTable(从SQL Server中提取)上达到列或行限制时,我想将DataTable拆分为子项,每个子项都遵守限制(对于.xls 256列和65,536行以及.xlsx 16,384列和1,048,576行。

到目前为止,我已经编写了以下方法来做我想要的事情

public static List<DataTable> SplitDataTable(DataTable mother, int nColLimit)
{
    List<int[]> rangeList = new List<int[]>();
    int primaryCols = mother.Columns.Count;
    int nSplitCount = Convert.ToInt32(primaryCols / nColLimit);
    int max = -1;

    // Get the child ranges.
    int tmpSup = 0;
    for (int splits = 0; splits < nSplitCount; splits++)
    {
        if (rangeList.Count == 0)
            tmpSup = (splits + 1) * (nColLimit - 1);
        else
            tmpSup = rangeList[splits - 1][1] + nColLimit;
        rangeList.Add(new int[2] { splits * nColLimit, tmpSup });
        if (max < tmpSup)
            max = tmpSup;
    }
    rangeList.Add(new int[2] { ++max, primaryCols });

    // Build child DataTables.
    List<DataTable> childList = new List<DataTable>();
    int childIndex = 0;
    foreach (int[] range in rangeList)
    {
        childList.Add(new DataTable());
        for (int i = range[0]; i < range[1]; i++)
            for (int j = 0; j < mother.Rows.Count; j++)
                childList[childIndex].Rows[j][i] = mother.Rows[j][i];
        childIndex++;
    }
    return childList;
}

然而,这会抛出indexOutOfRangeException,并显示消息“位置0处没有行”。我理解错误的来源,但是将完整列从mother复制到孩子的最佳方法是什么?

我也试过

List<DataTable> childList = new List<DataTable>();
int childIndex = 0;
foreach (int[] range in rangeList)
{
    childList.Add(new DataTable());
    foreach(DataRow row in mother.Rows)
    {
        DataRow tmpRow = childList[childIndex].NewRow();
        for (int i = range[0]; i < range[1]; i++)
            tmpRow[i + 1] = row[i + 1];
    }
    childIndex++;
}

给出相同的范围异常。

感谢您的时间。

编辑:我是如何做这个短期的

foreach (int[] range in rangeList)
{
    childList.Add(new DataTable());
    string strSqlTmp =
    String.Format("declare @columns varchar(max) " +
        "select  @columns = case when @columns is null " +
        "then '' " +
        "else @columns + ', ' " +
        "end + name " +
             "from sys.columns " +
             "where object_id = object_id('{0}') and name in " +
                 "(SELECT COLUMN_NAME " +
                     "FROM [{1}].INFORMATION_SCHEMA.COLUMNS " +
                     "WHERE TABLE_NAME = N'{0}' " +
                     "AND ORDINAL_POSITION > {2} AND ORDINAL_POSITION < {3}) " +
        "declare @query varchar(max) " +
        "set @query = 'select ' + @columns + ' from {0}' " +
        "exec (@query);
    // Then get each DataTable from SQL Server and fill the child list...

2 个答案:

答案 0 :(得分:1)

您收到IndexOutOfRange例外的原因是因为您尝试引用新Column中不存在的DataTable。这一行:

childList.Add(new DataTable()); 

确实向DataTable添加了新的childList,但DataTable没有列和行。

通常我可以使用DataTable.Clone()方法创建一个新的DataTable,其结构与调用该方法的DataTable相同,但显然这对您不起作用。在您的情况下,您必须使用DataTable.Columns.Add方法明确添加DataColumn

类似的东西:

for (int i = 0; i < numberOfColumns; i++) {
    dataTable.Columns.Add(string.Format("Column {0}", i));
}

上面的代码段非常简单。由于您将自己创建DataColumn,因此您需要自己命名并输入每个DataColumn

答案 1 :(得分:1)

我刚刚创建了一个拆分数据表的方法。 “.Batch”方法是从MoreLinq引用的。

private static List<DataTable> SplitTable(DataTable originalTable, int batchSize)
        {
            List<DataTable> tables = new List<DataTable>();

            foreach (var rowBatch in originalTable.Rows.Cast<DataRow>().Batch(batchSize))
            {
                var batchTable = new DataTable(originalTable.TableName);

                foreach (DataColumn column in originalTable.Columns)
                    batchTable.Columns.Add(column.ColumnName, column.DataType);

                foreach (DataRow row in rowBatch)
                    batchTable.Rows.Add(row.ItemArray);

                tables.Add(batchTable);
            }
            return tables;
        }

以防这对任何人都有帮助:)