【问题标题】:how to split datatable into multiple datatable based on row count in C# [closed]如何根据C#中的行数将数据表拆分为多个数据表[关闭]
【发布时间】:2017-04-24 18:14:13
【问题描述】:

如何根据C#中的行数将datatable拆分为多个datatable。请给我建议 .

MySqlDataAdapter da = new MySqlDataAdapter("", con);
                    da.SelectCommand.Parameters.AddWithValue("@sid", specializationId);
                    da.SelectCommand.Parameters.AddWithValue("@uid", userId);
                    da.SelectCommand.Parameters.AddWithValue("@counter", counter);
                    da.Fill(dt);

【问题讨论】:

  • 你真正想做什么?分页?
  • 1.格式化或删除该查询,因为没有人能够将其读取为 1 大行。 2. 提出问题而不仅仅是标题和代码转储

标签: c#


【解决方案1】:

你知道如何使用数据库分页吗?那么问题是什么?只需填写一个DataTable,将此DataTable 存储在List<DataTable>DataSet 中,然后选择下一个行集到另一个DataTable 中并将其添加到集合中。

如果您已经用所有行填充了一个很大的DataTable,并且您想将其拆分为多个DataTables,则此代码可以:

int tableSize = 100;               // for example
DataSet allTables = new DataSet(); // or List<DataTable>

for (int i = 0; i < bigTable.Rows.Count; i += tableSize)
{
    DataTable tbl = bigTable.Clone(); // same columns, empty
    for (int ii = 0; ii < tableSize; ii++)
    {
        if (i + ii == bigTable.Rows.Count) break;
        tbl.ImportRow(bigTable.Rows[i + ii]);
    }

    allTables.Tables.Add(tbl);
}

【讨论】:

  • 我想要每张 20 行的表格。如果数据表的行数为 100,则它分为 5 个表
  • @GirishPatil:好吧,那就把tableSize = 100改成tableSize = 20
  • 你能看看我的question 之一吗?
猜你喜欢
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多