473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Updating Rows for a large DataTable

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:
Expand|Select|Wrap|Line Numbers
  1. protected virtual void SetEditStamps(ref DataSet ds)
  2.         {
  3.             int count = 0;
  4.             if ( ds != null )
  5.             {
  6.                 foreach (DataTable dt in ds.Tables)
  7.                 {
  8.                     if ( dt.Columns.Contains(_dcnameEditId) && dt.Columns.Contains(_dcnameEditDate) )
  9.                     {
  10. //                        foreach (DataRow dr in dt.Select(null, null, DataViewRowState.ModifiedCurrent | DataViewRowState.Added))
  11.                         string sEditID = BusinessLogicComponent._dcnameEditId;
  12.                         string sEditDate = BusinessLogicComponent._dcnameEditDate;
  13.                         int iEditID = dt.Columns[sEditID].Ordinal;
  14.                         int iEditDate = dt.Columns[sEditDate].Ordinal;
  15.                         for (int i = 0; i<dt.Rows.Count; i++)
  16.                         {
  17. //                            SetRowLevelEditStamps(dt.Rows[i]);
  18.                             dt.Rows[i][iEditID]    = _lastEditBy;
  19.                             dt.Rows[i][iEditDate]    = _lastEditOn;
  20.                             count++;
  21.                         }
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.  
Is there a faster way to do this?

Thanks,
Simran
Mar 24 '08 #1
13 2123
nateraaaa
663 Expert 512MB
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++;
}
}
}
}
}
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
Mar 25 '08 #2
Plater
7,872 Expert 4TB
Here's what I reduced it to:
Expand|Select|Wrap|Line Numbers
  1. protected virtual void SetEditStamps(ref DataSet ds)
  2. {
  3.     //int count = 0;//count total from all tables? 
  4.  
  5.     //you only need to grab the string name once, not every time
  6.     string sEditID = BusinessLogicComponent._dcnameEditId;
  7.     string sEditDate = BusinessLogicComponent._dcnameEditDate;
  8.     if (ds != null)
  9.     {
  10.       foreach (DataTable dt in ds.Tables)
  11.       {
  12.         if (dt.Columns.Contains(sEditID) && dt.Columns.Contains(sEditDate))
  13.         {
  14.           for (int i = 0; i < dt.Rows.Count; i++)
  15.           {
  16.             //SetRowLevelEditStamps(dt.Rows[i]);
  17.  
  18.             //you can refer to columns by string name
  19.             dt.Rows[i][sEditID] = _lastEditBy;
  20.             dt.Rows[i][sEditDate] = _lastEditOn;
  21.             //count++;//you don't use it?
  22.           }
  23.         }
  24.       }
  25.     }
  26. }
  27.  
Mar 25 '08 #3
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,

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
Mar 25 '08 #4
Here's what I reduced it to:
Expand|Select|Wrap|Line Numbers
  1. protected virtual void SetEditStamps(ref DataSet ds)
  2. {
  3.     //int count = 0;//count total from all tables? 
  4.  
  5.     //you only need to grab the string name once, not every time
  6.     string sEditID = BusinessLogicComponent._dcnameEditId;
  7.     string sEditDate = BusinessLogicComponent._dcnameEditDate;
  8.     if (ds != null)
  9.     {
  10.       foreach (DataTable dt in ds.Tables)
  11.       {
  12.         if (dt.Columns.Contains(sEditID) && dt.Columns.Contains(sEditDate))
  13.         {
  14.           for (int i = 0; i < dt.Rows.Count; i++)
  15.           {
  16.             //SetRowLevelEditStamps(dt.Rows[i]);
  17.  
  18.             //you can refer to columns by string name
  19.             dt.Rows[i][sEditID] = _lastEditBy;
  20.             dt.Rows[i][sEditDate] = _lastEditOn;
  21.             //count++;//you don't use it?
  22.           }
  23.         }
  24.       }
  25.     }
  26. }
  27.  
Plater,

The reason I was using ordinals to access columns because I was hoping it would make the row access faster. It didn't..
And as I explained in my other reply, the count is there just so I can see how many rows it has looped through in a set period of time.

Thanks,
Simran
Mar 25 '08 #5
Plater
7,872 Expert 4TB
Just how long are we talking here? I mean 400,000 rows is going to take some time to sift through, regardless of speed.
Mar 25 '08 #6
Just how long are we talking here? I mean 400,000 rows is going to take some time to sift through, regardless of speed.
Its taking like 15 secs for going through about 60-70 rows...

Thanks,
Simran
Mar 25 '08 #7
Just how long are we talking here? I mean 400,000 rows is going to take some time to sift through, regardless of speed.
Plater,

Also see my reply to Nathan above. I have noticed that once in a while, apparently randomly, it'll go through the entire 400,000 rows in a matter of seconds. I'm still trying to figure out why. I'm wondering if it is something to do with rebuilding references, or something else...but I want to find a way to recreate that.

Thanks,
Simran
Mar 25 '08 #8
Plater
7,872 Expert 4TB
Its taking like 15 secs for going through about 60-70 rows...

Thanks,
Simran
Oh wow, I would have thought it could do the whole thing in that amount of time.
What is done in:
SetRowLevelEditStamps
maybe that is eating up time?
Mar 25 '08 #9
SpecialKay
109 100+
400,000 rows is really not that much. I would think that it should not take more then 30-60 second to process that. Try debugging, maybe you can see where the slow down is.
Mar 25 '08 #10
Oh wow, I would have thought it could do the whole thing in that amount of time.
What is done in:
SetRowLevelEditStamps
maybe that is eating up time?
Plater,

The the SetRowLevelEditStamps method call has been commented out. All that was being done in there is now being done in the loop itself with the updating of the rows.

Thanks,
Simran
Mar 25 '08 #11
400,000 rows is really not that much. I would think that it should not take more then 30-60 second to process that. Try debugging, maybe you can see where the slow down is.
Kay,

I have tried debugging :) of course! But do you have any suggestions as to where to look for potential problems?

thanks,
Simran
Mar 25 '08 #12
SpecialKay
109 100+
Well, looking at the code you provided, it all seems very basic, nothing i can see would cause a slow down. So the problem is going to be in the BusinessLogicComponent class. Its going to be hard to narrow down. Wish i could be more help.
Mar 26 '08 #13
Well, looking at the code you provided, it all seems very basic, nothing i can see would cause a slow down. So the problem is going to be in the BusinessLogicComponent class. Its going to be hard to narrow down. Wish i could be more help.

Everyone,

Thanks very much for your help. I think the problem lies somewhere in .Nets memory management and I'll keep on digging into it.

Simran
Mar 28 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Luis Esteban Valencia | last post by:
Hello Everyone, Iam an intermediate ASP.Net programmer and iam facing a challenging task. I have a table in MS-SQL server database called 'Members'. The table has following fields... ...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
3
by: RSH | last post by:
Hi, I have a situation in where i have two instances of SQL server, the first is our Production Environment, the second is our Development environment. Both servers contain the same databases...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
3
by: nguyenlh | last post by:
code: I have read a example :The use girdview without datasource <asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit"...
0
by: Chet | last post by:
I have a Datagrid that is bound to a Datatable at runtime. I allow the user to select a number of rows using the mouse and then click a button that says "check selected rows", which then cycles...
1
by: Wavey | last post by:
Hi All, I have a problem with updating an Access database from a datatable. I have two rows of data in my database at the moment for testing, the first row is an ID number (primary key), the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.