更新大型DataTable的行 [英] Updating Rows for a large DataTable

查看:55
本文介绍了更新大型DataTable的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要找到一种更新大约400,000行内存数据表的方法。循环遍历非常慢。

这里是我正在使用的代码:

Hi,

I need to find a way to update some 400,000 rows of an in-memory datatable. Looping through rows is very slow.
here''s the code I''m using:

展开 | 选择 | 换行 | 行号

推荐答案


protected virtual void SetEditStamps(ref DataSet ds)

{

int count = 0;

如果(ds!= null)

{

foreach(数据表dt in ds.Tables)

{

if(dt.Columns.Contains(_dcnameEditId)&& dt.Columns.Contains(_dcnameEditDate))

{

// foreach(DataRow dr i n dt.Select(null,null,DataViewRowState.ModifiedCurrent | DataViewRowState.Added))

string sEditID = BusinessLogicComponent._dcnameEditId;

string sEditDate = BusinessLogicComponent._dcnameEditDate;

int iEditID = dt.Columns [ sEditID] .Ordinal;

int iEditDate = dt.Columns [sEditDate] .Ordinal;

for(int i = 0; i< dt.Rows.Count; i ++)

{

// SetRowLevelEditStamps(dt.Rows [i]);

dt.Rows [i] [iEditID] = _lastEditBy;

dt.Rows [i] [iEditDate] = _lastEditOn;

count ++;

}

}

}

}

}
protected virtual void SetEditStamps(ref DataSet ds)
{
int count = 0;
if ( ds != null )
{
foreach (DataTable dt in ds.Tables)
{
if ( dt.Columns.Contains(_dcnameEditId) && dt.Columns.Contains(_dcnameEditDate) )
{
// foreach (DataRow dr in dt.Select(null, null, DataViewRowState.ModifiedCurrent | DataViewRowState.Added))
string sEditID = BusinessLogicComponent._dcnameEditId;
string sEditDate = BusinessLogicComponent._dcnameEditDate;
int iEditID = dt.Columns[sEditID].Ordinal;
int iEditDate = dt.Columns[sEditDate].Ordinal;
for (int i = 0; i<dt.Rows.Count; i++)
{
// SetRowLevelEditStamps(dt.Rows[i]);
dt.Rows[i][iEditID] = _lastEditBy;
dt.Rows[i][iEditDate] = _lastEditOn;
count++;
}
}
}
}
}



您是否考虑过将DataTable作为参数传递给这个方法而不是整个数据集?在调用此方法之前,您可以执行逻辑以确定您具有正确的数据集表,然后将正确的数据表作为参数传递给此方法。在该方法中,您将只有1个foreach循环而不是2.这应该减少此方法的处理时间。尝试一下,如果遇到任何问题,请告诉我们。


Nathan

Have you considered passing a DataTable as a parameter to this method instead of the entire dataset? You could do your logic to identify you have the correct dataset table before calling this method then just pass the correct datatable to this method as a parameter. In the method you would then have only 1 foreach loop instead of 2. This should reduce the process time for this method. Give this a try and let us know if you run into any problems.

Nathan


这是我把它缩减为:
Here''s what I reduced it to:
展开 | 选择 | Wrap | 行号



您是否考虑将DataTable作为参数传递给此方法而不是整个数据集?在调用此方法之前,您可以执行逻辑以确定您具有正确的数据集表,然后将正确的数据表作为参数传递给此方法。在该方法中,您将只有1个foreach循环而不是2.这应该减少此方法的处理时间。尝试一下,如果遇到任何问题,请告诉我们。


Nathan
Have you considered passing a DataTable as a parameter to this method instead of the entire dataset? You could do your logic to identify you have the correct dataset table before calling this method then just pass the correct datatable to this method as a parameter. In the method you would then have only 1 foreach loop instead of 2. This should reduce the process time for this method. Give this a try and let us know if you run into any problems.

Nathan



Nathan,


感谢您的回复。我试过了,但它并没有太大的区别。数据集中只有大约4个表,因此它不会产生非常大的循环。另外,它只是循环通过一个数据表的行很慢。 ''count''变量就是这样,我可以看到它在一段时间内产生了多少行。目前它需要大约15秒才能完成大约60行,你可以看到它很慢。但是我注意到这里有些有趣。有一段时间......就像10-15次尝试一样,它会在几秒钟内完成整个表格!我还在试图找出为什么会发生这种情况,更重要的是,我怎样才能让这种事情发生。

任何其他建议都会非常受欢迎!


再次感谢,

Simran


Nathan,

Thanks for your reply. I tried it, but it didnt make too much of a difference. There are only about 4 tables in the dataset and so it doesnt make for a very big loop. Plus, its just looping through the rows of the one datatable thats very slow. The ''count'' variable is there just so I can see how many rows it has churned through in a set period of time. Currently its taking around 15 secs to go through about 60 rows which you can see is painfully slow. But here''s something funny I noticed. Once in a while...just once in like 10-15 tries, it''ll just go through the entire table in a matter of seconds! I''m still trying to figure out why that''s happening and more importantly, how I can get that to happen everytime.
Any other suggestions would be very welcome!

Thanks again,
Simran


这篇关于更新大型DataTable的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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