473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fastst way to merge 2 DataTables/DataViews/...

Hi,

I have two DataTables (our DataViews or whatever that will suit the best for
the solution). I want to merge these two DataTables the fastest as possible,
but they have to be merged one table after the others: First all the recors
of DataTable1, and afterwarths the records of DataTable2.

Does anybody has any idea how to do this? The purpose it that it goes as
fast as possible! DataTable2 can have up to 15.000 records, DataTable1
100-200 records. The order of the records is really important: The records
of DataTable1 must come before those of DataTable2, and all of htem has to
be in the order they had orignaly in their DataTable.

Any help would be really appreciated! I tried with InsertAt, Import, Merge
etc, but none of them seemd really quick to mee :-/

Thanks a lot in advance,

Pieter
Nov 21 '05 #1
9 20870
Pieter,

I think that I would create my own merge, by reading accessing all the
datatables in the same time and write them only once to a *new* table if
the comparassing tells that it should and otherwise skip them.

A very old way of datatape processing.

I hope this helps to give you idea's,

Cor
Nov 21 '05 #2
How do you mean? there shouldn't be any comparisson at all! Just put all the
records in one DataTable/DataView/DataSet/ whatever...

Like this:
DataTable1
-----------
Record1A
Record1B
Record1C

DataTable2
------------
Record2A
Record2B

DataTableResult (which may be DataTable1 or DataTable2 or whatever)
-------------------------------------------------------------------------
Record1A
Record1B
Record1C
Record2A
Record2B

This is what shoudl happen: nothing more, nothing less: ALL the records of
NOTH DataTables should be in the (eventually) new DataTable, in the SAME
ORDER they were before, ONE DataTable AFTER ANOTHER...

Any idea how to do this as fast as possible?

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:uV******** ******@TK2MSFTN GP14.phx.gbl...
Pieter,

I think that I would create my own merge, by reading accessing all the
datatables in the same time and write them only once to a *new* table if
the comparassing tells that it should and otherwise skip them.

A very old way of datatape processing.

I hope this helps to give you idea's,

Cor

Nov 21 '05 #3
Pieter,

Than it is even copy the first table and loop to the others while importing
the datarows to that table copied table.

Something as

dim dtBasis as Datatable = DtWaarMeeTeStar ten.Copy
toevoegroutine( dtBasis, TabellekeOmToeT eVoegen)
'To do for every datatable except the first one

\\\
private sub toevoegroutine( byval dt as datatable, byval Patat as DataTable)
for each dr as datarow in Patat.rows
dt.importrow(dr Patat)
Next
///

End sub

I hope that is what you mean

Cor
Nov 21 '05 #4
Hi Cor,

This is indeed something I want, but the problem is that it doesn't go fast
enough... Adding 200 rows like this takes easily 2 seconds... It should take
only 0.01 second :-)

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:eR******** ******@tk2msftn gp13.phx.gbl...
Pieter,

Than it is even copy the first table and loop to the others while
importing the datarows to that table copied table.

Something as

dim dtBasis as Datatable = DtWaarMeeTeStar ten.Copy
toevoegroutine( dtBasis, TabellekeOmToeT eVoegen)
'To do for every datatable except the first one

\\\
private sub toevoegroutine( byval dt as datatable, byval Patat as
DataTable)
for each dr as datarow in Patat.rows
dt.importrow(dr Patat)
Next
///

End sub

I hope that is what you mean

Cor

Nov 21 '05 #5
Pieter,

What is it you use as computer a 'telraam' system.

Can you try this sample and tell me how much milliseconds you was using. If
I did not make an error, than it creates a table with 100 filled columns and
15000 rows by steps from 100 in a time.

\\\
Public Class Main
Public Shared Sub Main()
Dim start As Integer = Environment.Tic kCount
Dim dt As New DataTable
For i As Integer = 0 To 99
dt.Columns.Add( )
Next
For x As Integer = 1 To 100
For i As Integer = 1 To 150
Dim dr As DataRow = dt.NewRow
For j As Integer = 0 To 99
dr(j) = "Pieter"
Next
dt.Rows.Add(dr)
Next
Next
MessageBox.Show ((Environment.T ickCount - start).ToString )
End Sub
End Class
//

:-)

Cor

Nov 21 '05 #6
Werid, it took only 550-650 ticks each time... That's so much less than the
results I got? I even used DataTable.Begin LoadData and EndLoadData...
I guess I'll have to review some stuff, hehe :) Thanks a lot Cor! You did
that hartstikke good!

Was there any reason why you did that in steps of 100 times 150? I didn't
notice any remarkable changes on first sight when changing these parameters?
Should they?

Thanks,

Pieter
"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:u4******** ******@TK2MSFTN GP09.phx.gbl...
Pieter,

What is it you use as computer a 'telraam' system.

Can you try this sample and tell me how much milliseconds you was using.
If I did not make an error, than it creates a table with 100 filled
columns and 15000 rows by steps from 100 in a time.

\\\
Public Class Main
Public Shared Sub Main()
Dim start As Integer = Environment.Tic kCount
Dim dt As New DataTable
For i As Integer = 0 To 99
dt.Columns.Add( )
Next
For x As Integer = 1 To 100
For i As Integer = 1 To 150
Dim dr As DataRow = dt.NewRow
For j As Integer = 0 To 99
dr(j) = "Pieter"
Next
dt.Rows.Add(dr)
Next
Next
MessageBox.Show ((Environment.T ickCount - start).ToString )
End Sub
End Class
//

:-)

Cor

Nov 21 '05 #7
Well thansk Cor, you helped me a lot! In my application, which has a
DataTable with only 2 columns it takes like 50 ticks to merge a DataRow() of
200 records and a DataTable of 12000 records! So really a great performance.

I now have another part that becomes the bottleneck: adding the datatable to
the combobox: It teks easily 850 ticks... any idea to get it better?

Here's my code:

Private Sub AddSearchedToLi st()
Dim dtbl As New DataTable
dtbl = MyWorkSpace.Get AllArticlesClie nt(61).Copy

Dim drows() As DataRow
drows = dtbl.Select("No mArticle LIKE '*HUILE*'", "NomArticle ")

dtbl = Me.InsertDataRo ws(drows, dtbl)

'add to the combo
Dim start As Integer = Environment.Tic kCount
cmbTest.Suspend Layout()
cmbTest.BeginUp date()
Me.cmbTest.Data Source = dtbl 'dtv
Me.cmbTest.Disp layMember = "NomArticle "
Me.cmbTest.Valu eMember = "CodeArticl e"
cmbTest.EndUpda te()
cmbTest.ResumeL ayout()
MessageBox.Show ((Environment.T ickCount - start).ToString ) '-> +- 850
End Sub

Private Function InsertDataRows( ByVal drows() As DataRow, ByVal dtbl2 As
DataTable) As DataTable
Dim dtbl As New DataTable
Dim dr As DataRow
Dim intX, intY As Integer

Dim start As Integer = Environment.Tic kCount

'For intX = 0 To intColumns
' dtbl.Columns.Ad d()
'Next
dtbl.Columns.Ad d("CodeArticle" )
dtbl.Columns.Ad d("NomArticle ")

dtbl.BeginLoadD ata()

'first dtbl1
For intX = 0 To (drows.Length - 1)
dr = dtbl.NewRow
For intY = 0 To 1
dr(intY) = drows(intX).Ite m(intY)
Next
dtbl.Rows.Add(d r)
Next

'add the lines betweeen the 2 datatables...
dr = dtbl.NewRow
dr(1) = " ------------------------"
dtbl.Rows.Add(d r)

'than dtbl2
For intX = 0 To (dtbl2.Rows.Cou nt - 1)
dr = dtbl.NewRow
For intY = 0 To 1
dr(intY) = dtbl2.Rows(intX ).Item(intY)
Next
dtbl.Rows.Add(d r)
Next

dtbl.EndLoadDat a()

MessageBox.Show ((Environment.T ickCount - start).ToString ) '-> +-50

Return dtbl

End Function
"DraguVaso" <pi**********@h otmail.com> wrote in message
news:OT******** ******@TK2MSFTN GP14.phx.gbl...
Werid, it took only 550-650 ticks each time... That's so much less than
the results I got? I even used DataTable.Begin LoadData and EndLoadData...
I guess I'll have to review some stuff, hehe :) Thanks a lot Cor! You did
that hartstikke good!

Was there any reason why you did that in steps of 100 times 150? I didn't
notice any remarkable changes on first sight when changing these
parameters? Should they?

Thanks,

Pieter
"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:u4******** ******@TK2MSFTN GP09.phx.gbl...
Pieter,

What is it you use as computer a 'telraam' system.

Can you try this sample and tell me how much milliseconds you was using.
If I did not make an error, than it creates a table with 100 filled
columns and 15000 rows by steps from 100 in a time.

\\\
Public Class Main
Public Shared Sub Main()
Dim start As Integer = Environment.Tic kCount
Dim dt As New DataTable
For i As Integer = 0 To 99
dt.Columns.Add( )
Next
For x As Integer = 1 To 100
For i As Integer = 1 To 150
Dim dr As DataRow = dt.NewRow
For j As Integer = 0 To 99
dr(j) = "Pieter"
Next
dt.Rows.Add(dr)
Next
Next
MessageBox.Show ((Environment.T ickCount - start).ToString )
End Sub
End Class
//

:-)

Cor


Nov 21 '05 #8
Pieter,

Why not do it in a way like this?

I know some reasons however I think that I than have a solution for that.

\\\
Dim dtbl As New DataTable
dtbl.Columns.Ad d("CodeArticle" , GetType(System. Int32))
dtbl.Columns.Ad d("NomArticle ")
For i As Integer = 1 To 3
dtbl.LoadDataRo w(New Object() {i, "Huitr1-3"}, True)
Next
For i As Integer = 5 To 10000
dtbl.LoadDataRo w(New Object() {i, "Moules"}, True)
Next
For i As Integer = 10001 To 15000
dtbl.LoadDataRo w(New Object() {i, "HuitrEnd"} , True)
Next
dtbl.DefaultVie w.RowFilter = "NomArticle LIKE '*HUItr*'"
dtbl.DefaultVie w.Sort = "CodeArticl e"
Me.cmbtest.Data Source = dtbl.DefaultVie w 'dtv
Me.cmbtest.Disp layMember = "NomArticle "
Me.cmbtest.Valu eMember = "CodeArticl e"
Dim start As Integer = Environment.Tic kCount
dtbl.LoadDataRo w(New Object() {4, "Huitr4"}, True)
MessageBox.Show ((Environment.T ickCount - start).ToString ) '-> +- 850
///

I hope this helps,

Cor
Nov 21 '05 #9
The performance of DataSet Merge is very poor with large numbers of rows.
The reason is that for each column name in the source table must be looked
up against each column name in the destination table for each row.
A better strategy for performance would be to first create a mapping table
for the source to destination columns and then use these when copying the
values from the rows.

Hope this helps,
Phil

P.S. I used Anakrino to disassemble the DataSet Merge code:
http://www.saurik.com/net/exemplar/
"DraguVaso" wrote:
Hi,

I have two DataTables (our DataViews or whatever that will suit the best for
the solution). I want to merge these two DataTables the fastest as possible,
but they have to be merged one table after the others: First all the recors
of DataTable1, and afterwarths the records of DataTable2.

Does anybody has any idea how to do this? The purpose it that it goes as
fast as possible! DataTable2 can have up to 15.000 records, DataTable1
100-200 records. The order of the records is really important: The records
of DataTable1 must come before those of DataTable2, and all of htem has to
be in the order they had orignaly in their DataTable.

Any help would be really appreciated! I tried with InsertAt, Import, Merge
etc, but none of them seemd really quick to mee :-/

Thanks a lot in advance,

Pieter

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3618
by: Ruslan Shlain | last post by:
I have aproblem when i merge datasets. dsDS has 105 records dsMain has 147 record I am trying to to pour records in dsMain in to dsDS. Here is my code: DataSet dsDS =new DataSet(); SqlConnection conSqlConn = new SqlConnection(m_strdatabaseconConn);
0
793
by: jhcorey | last post by:
I have datatable X with rows a,b,c and datatable Y with rows b,c. I'm trying to figure out a simple way to replace rows b and c in X with the ones in Y. Obviously I need a key column to match up the rows. Also obviously, I could iterate through each of the datatables, but I was wondering if there was some built-in functionality (like dataset.merge, which I don't really understand) that could simplify
1
1189
by: Bauer | last post by:
I have already implemented merge of 2 datasets.Would like to clarify if this merge does not break. I have 2 datasets each having tables. ds1 has dt1 and ds2 has dt2. dt1 and dt2 differ in only 1 column. Both the datatables do not have any primary keys.when i performed a merge on both the datasets everything works fine. I get a single table dt1 with the extra column from dt2. Is that a correct way of doing it?
5
13361
by: Frank | last post by:
Hello All, I am working on a vb.net app where I need to compare to 2 datatables and determine if a string exists in one or both. The first dt is filled from the db. A form is loaded and the appropriate items in a checkedlist box are selected based on the dt. So far, no problem. Then user can then edit the values in the checkedlist box and choose to save changes. When they save changes, I throw the new values of the checked items from...
0
1343
by: wapsiii | last post by:
How is it possible to combine/join/merge two identical (schema) datatables into a new datatable? /M
1
15786
by: mj2736 | last post by:
I'm a little confused about DataTable.Merge(). I have two DataTable objects with the same structure - dtOrig and dtCurrent. The end result I'm trying to achieve is to get all the rows in dtCurrent that are in some way different from the corresponding rows in dtOrig so those new/ modified/deleted rows can be processed. My idea was to use dtOrig.Merge(dtCurrent), and then call dtOrig.GetChanges(). But what I found is that when the...
1
2191
by: Sam Shrefler | last post by:
Is there any code part of the .NET Framework or any suggestions at the best way to handle the following situation? Results from SQL Select statement: MeasureDate | Name | Width | Height 01/01/07 | Flower | 10 | 10 01/01/07 | Tree | 11 | 11 01/01/07 | Bush | 12 | 12 02/01/07 | Flower | 11 | 10
0
1109
by: kevin.jennings | last post by:
Hi! I'm an "old-school" programmer used to dealing with data one record at a time (using old RPG code like 'chain' and 'read' statements). I'm not used to dealing with huge chunks of data at one time. Nowadays, however, it seems that the modern languages like Java and C# steer you towards working with data in a 'disconnected' fashion, i.e. loading records into datatables and datasets then using dataviews to look at them how you like. ...
1
1386
by: rajch | last post by:
Hi, Can anyone tell me, how to merge two datatables within a dataset. I want to merge two different table data into a single table within a dataset. Thanks Raj
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.