如何将一个数据表拆分为多个数据表? [英] How to Split one datatable to many datatables?

查看:110
本文介绍了如何将一个数据表拆分为多个数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!祝你有美好的一天
我有一个问题,请帮帮我!
我想将数据表拆分为许多数据表,如何通过c#.net做到这一点.
示例:数据表为:DA有250行==>拆分为3个数据表:DA1(100行),DA2(100行),DA3(50行)
如果数据表DA具有1000行,则==>拆分为10个数据表(这意味着一个数据表的行数少于100行)

感谢您的帮助!

Hi !! Have a good day

I have a question, please help me !
I want to split a datatable into many datatables, how to do that by c#.net.
Example : datatable is: DA has 250 rows ==> split into 3 datatable : DA1 (100rows) , DA2(100 rows), DA3(50rows)
If datatable DA has 1000 rows, ==> split into 10 datatables, ( thats mean one datatable have less than 100rows)

Thanks for your help!

推荐答案

您将不得不遍历行并将其复制到新的数据表中.您也可以使用DataTable.Select方法,而不是遍历所有行.

You will have to loop through the rows and copy them into a new datatable. You can also use DataTable.Select method as well instead of looping through all the rows.


最简单的方法是编写一个循环遍历DataTable并相应地分配行的循环.执行此操作时,您需要克隆表结构,并使用ImportRow将行从一个表复制到另一个表.如果尝试直接复制该行,则会收到行属于另一个表"错误.以下方法可以满足您的需求:
The simplest way would be to write a loop that iterated over the DataTable and allocate rows accordingly. When you do this, you need to clone the table structure, and use ImportRow to copy the row from one table to another. If you attempt to copy the row over directly, you''ll get a "row belongs to another table" error. The following method does what you want:
private List<DataTable> CloneTable(DataTable tableToClone, int countLimit)<br />{<br />    List<DataTable> tables = new List<DataTable>();<br />    int count = 0;<br />    DataTable copyTable = null;<br />    foreach (DataRow dr in tableToClone.Rows)<br />    {<br />        if ((count++ % countLimit) == 0)<br />        {<br />            copyTable = new DataTable();<br />            // Clone the structure of the table.<br />            copyTable = tableToClone.Clone();<br />            // Add the new DataTable to the list.<br />            tables.Add(copyTable);<br />        }<br />        // Import the current row.<br />        copyTable.ImportRow(dr);<br />    }<br />    return tables;<br />}






这篇关于如何将一个数据表拆分为多个数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆