如何多次删除第一行数据表 [英] How ro delete the first row of data table multiple time

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

问题描述

嗨朋友们,



我有一个包含10000行和20列的大型数据表。

我有一个方法可以读取第一行的数据。一旦我读取数据,我需要删除第一行。所以在下次通话时我会得到正确的数据。

当我第二次尝试调用该方法时会发出错误



无法通过行访问已删除的行信息



如何解决此问题。请帮助



我尝试过的事情:



Hi Friends,

I have a large datatable which contain 10000 rows and 20 columns.
I have a method which will read the data from the first row. Once i read the data i need to remove the first row. so on the next call i will get the correct data.
When i try to call the method second time its give an error

Deleted row information cannot be accessed through the row

How can i solve this. please help

What I have tried:

public static double[] ReadPresimulatedData()
        {
 double[] preValues = Array.ConvertAll<object, double>(dsPresimulated.Tables[0].Rows[0].ItemArray, o => (double)o);
dsPresimulated.Tables[0].Rows[0].Delete();
return preValues;
        }

推荐答案

您无法更改当前正在循环的任何可枚举对象。你会收到这样的错误以及更多错误。



为什么在解析它们后删除行?为什么不循环然后处理整个表?给出的解释没有任何意义。你的意思是删除符合条件的行吗?然后,在完成循环后,将顶部删除它们,或者将要保留的行添加到DataTable的新实例。



如果没有然后请帮助详细描述您正在尝试做什么以及如何循环遍历行。这些要点与我们提供的任何解决方案相关。



谢谢^ _ ^

Andy
You can't change any enumerable object your are currently looping through. You get errors like this and many more.

Why delete rows once you've parsed them? Why not just loop through then dispose of the whole table? It makes no sense with the explanation given. Do you mean that you are delete rows which meet a condition? Then you will have top remove them after you have finished your loop, or add the rows you want to keep to a new instance of a DataTable.

If this doesn't help then please describe in detail what you are trying to do and how you are looping through the rows. These points are relevant to any solution we can offer.

Thanks ^_^
Andy


调用删除将行的状态设置为已删除。它不会从表中删除行。当您想要对内存中的数据进行一些更改然后将它们保存到数据库时,可以使用此选项。该表需要知道您删除了哪些行才能从数据库中删除它们。



如果您只想删除该行,请使用:

Calling Delete sets the row's state to Deleted. It doesn't remove the row from the table. This is intended to be used when you want to make some changes to the data in-memory and then persist them to the database; the table needs to know which rows you've deleted in order to delete them from the database.

If you just want to remove the row, then use:
dsPresimulated.Tables[0].Rows.RemoveAt(0);


在队列中加载数据然后当您从队列中取出每个项目并处理它从数据库中删除它。您也可以使用堆栈,取决于您是否需要LIFO或FIFO。



在处理之前,请不要将其全部加载到队列中并从数据库中删除;如果你的代码崩溃,你将失去一切。



另一种方法是SELECT TOP 1 * FROM TABLE并一次处理一个项目然后将其删除。



数据库调用和加载到内存之间的平衡,这个小的设计决定后来又困扰着你。
Load the Data in a Queue then as you take each item off the queue and process it delete it from the database. You could also use a stack , depends if you want LIFO or FIFO.

Don't load it all into the queue and deleted from the data base before you process it; cos if your code crashes you will lose it all.

Another way is to "SELECT TOP 1 * FROM TABLE" and process the items one at a time then deleted it.

Balance between database calls and loading into memory, its this little design decisions that come back to haunt you later.


这篇关于如何多次删除第一行数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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