|
-
Nov 14th, 2011, 07:10 AM
#1
Thread Starter
Frenzied Member
-
Nov 14th, 2011, 12:45 PM
#2
Re: Quick way to update datatable
There may or may not be a better way to do this. At first glance, it looks like it would be better to only update the records that are new or changed, but that may not be efficient. After all, for this to work, you would have to know which records needed to be updated. If you have some easy way to know this, and the number is small, then just updating those would be simpler. If you have to check each record to determine whether or not it has changed, then the cost of checking will outweigh any possible gain.
What isn't clear to me is why you need to do this in the first place. You are getting a datatable back from the service, why can't you replace the existing with this new table. If all you have to do is add a column and populate it, this is probably still going to be faster than copying over all the records.
My usual boring signature: Nothing
 
-
Nov 14th, 2011, 01:02 PM
#3
Thread Starter
Frenzied Member
Re: Quick way to update datatable
Thanks for the answer.
To clarify, this table it's a table where i have the list of all materials used by the company, so i need to read all the rows, because i have 3 fields that could have changed, descriptions, quantity available and other numeric field. So the fastest solution it's to clear the original (truncate) and insert all new rows...
So what i need is, clear the table and then insert all rows.
I was trying to use an adapter to do the job, but behind the scene what it does it's calling the insert command for each row, doesnt it?
The current code that i'm using it 's a simple for each datarow in the datatable, and add the correct parameters related to the correct cell and call the executenonquery. I don't like this, doesn't look like an elegant way to do the job, and takes some time to finish... i have the sensation that the adapter may do the job faster.
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Nov 14th, 2011, 01:17 PM
#4
Re: Quick way to update datatable
It would call the Insert command if you were using the adapter to update the database table with all new records, but I thought you were just talking about a datatable. Then you mentioned the ExecuteNonQuery, which sounds like a database table again. So what are we actually talking about, a table in the database, or a datatable object in memory?
My usual boring signature: Nothing
 
-
Nov 14th, 2011, 01:31 PM
#5
Thread Starter
Frenzied Member
-
Nov 14th, 2011, 01:56 PM
#6
Re: Quick way to update datatable
Oh. In that case, you may be doing about as well as you can. The adapter will indeed call an INSERT on each row. Is that in any way better than you doing that yourself? That's hard to say. I haven't specifically tested that, but I do note that the WHERE clause in the adapter can be really ugly if you are using a CommandBuilder to generate it automatically. If you write it yourself, then there shouldn't be any difference at all between the two methods.
My usual boring signature: Nothing
 
-
Nov 14th, 2011, 03:11 PM
#7
Thread Starter
Frenzied Member
Re: Quick way to update datatable
Hehehe
I have to test the two methods, and see what i got, and i write my one insert.
Thanks
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Nov 15th, 2011, 10:04 AM
#8
Thread Starter
Frenzied Member
Re: Quick way to update datatable
Hello
I had tested both methods, and they take aprox. the same time, 16 seconds to fill around 30k rows.
But now i take this one step further, in the original datatable all fields are strings, and in my datatable i want to put the fields in the correct format, so before setting the parameter value I use the cdec function, and now it takes around 3 minutes to do the job, if i use the cdbl it takes 5.40 minutes, and using the double/decimal parse with a string replace it takes arround 20 seconds... I could only do this with the loop version, if i use the adapter i get always a conversion error from string to decimal (the strings that i get from the original datatable use the comma to separate the units)...
Thanks
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Nov 15th, 2011, 10:31 AM
#9
Re: Quick way to update datatable
Yeah, converting strings is going to hurt. Is there any way you could avoid that first table having just strings?
My usual boring signature: Nothing
 
-
Nov 15th, 2011, 11:26 AM
#10
Thread Starter
Frenzied Member
-
Nov 15th, 2011, 12:29 PM
#11
Thread Starter
Frenzied Member
Re: Quick way to update datatable
Just to close this thread.
I managed to gain another 6/7 seconds, replacing the insert statement by a sproc. So the final time it's around 13 seconds...
Rate People That Helped You
Mark Thread Resolved When Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|