|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Combining datatablesHey there, just starting out using data tables, can anyone tell me how
to combine 2 tables? Ive tried just coyping the info from 1 table to another, which both have exactly the same table layout data is a datatable data1 is a datatable data.Rows.Add(data1.Rows(0).Item(0)) but it dosnt work, can anyone tall me what im doing wrong? Not sure if I understand completely, but are you asking how you can
copy the CONTENTS of one datatable into another? Or do you want to MERGE the two tables, creating a new table that has columns from both? How are both tables constructed previously? Post code if necessary. Ahh sorry for not being clear,
I want to copy the contents of one table to another I have been playing around and have now tried Dim itemadd As DataRow = data.NewRow() itemadd = data1.Rows(0).Item(0) data.Rows.Add(itemadd) but does not work. tables were previously created via
Public Function jWildCard(ByVal table1 As String, ByVal returncolumn As String, ByVal table2 As String, ByVal checkcolumn As String, ByVal table1joinField As String, ByVal table2joinField As String, ByVal checkvalue As String) As DataTable ' Dim StringToReturn As String StringToReturn = "" Dim SQL As String SQL = "SELECT " + table1 + "." + returncolumn + " FROM " + table1 + " INNER JOIN " + table2 + " ON " + table1 + "." + table1joinField + " = " + table2 + "." + table2joinField + " WHERE(" + table2 + "." + checkcolumn + " LIKE '%" + checkvalue + "%')" Dim dataAdapter As System.Data.OleDb.OleDbDataAdapter dataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQL, Me.OleDbConnection1) Dim dt As System.Data.DataTable dt = New System.Data.DataTable dataAdapter.Fill(dt) Return dt this is done twice and both tables have the same Select table and return coloumn. Ok try this:
Dim newRow as datarow = secondtable.NewRow() newRow.ItemArray = oldRow.Itemarray() secondtable.Rows.add(newRow) .... where oldRow is a row from your old table. This code would be inserted in a 'for each' loop to get each row from the old table. Good luck. Steven,
If you are using the 2.0 framwork you can to the following: Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable Just make sure you don't have any rowfilters set on the default view. Cheers, Rob Panosh Steven Nagy wrote: Show quoteHide quote > Ok try this: > > Dim newRow as datarow = secondtable.NewRow() > newRow.ItemArray = oldRow.Itemarray() > secondtable.Rows.add(newRow) > > ... where oldRow is a row from your old table. > This code would be inserted in a 'for each' loop to get each row from > the old table. > > Good luck. Sorry mean't to send to Bonzol.
Cheers, Rob Rob Panosh wrote: Show quoteHide quote > Steven, > > If you are using the 2.0 framwork you can to the following: > > Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable > > Just make sure you don't have any rowfilters set on the default view. > > Cheers, > Rob Panosh > > > Steven Nagy wrote: > > Ok try this: > > > > Dim newRow as datarow = secondtable.NewRow() > > newRow.ItemArray = oldRow.Itemarray() > > secondtable.Rows.add(newRow) > > > > ... where oldRow is a row from your old table. > > This code would be inserted in a 'for each' loop to get each row from > > the old table. > > > > Good luck. Rob,
> Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable Dim newTable as DataTable = dt.copydoes the same without thinking about the rowfilter and version. http://msdn2.microsoft.com/en-us/library/system.data.datatable.copy.aspx I got the idea you did forever miss this one. (However this cannot be the solution from the problem from the OP) Cor Yes the OP asked about combining 2 tables.
We don't want to overwrite the second table with a copy of the first. Thus I think my solution might still be the best... Its untested code though so am not sure how the datarow will handle the itemarray if any of its copied contents do not match the columns collection in the parent table. Cor Ligthert [MVP] wrote: Show quoteHide quote > Rob, > > > Dim newTable as New System.Data.DataTable = dt.DefaultView.ToTable > Dim newTable as DataTable = dt.copy > > does the same without thinking about the rowfilter and version. > > http://msdn2.microsoft.com/en-us/library/system.data.datatable.copy.aspx > > I got the idea you did forever miss this one. > (However this cannot be the solution from the problem from the OP) > > Cor Steven,
> Yes the OP asked about combining 2 tables. Simple it throws an error and if not catched it stops.> We don't want to overwrite the second table with a copy of the first. > Thus I think my solution might still be the best... > Its untested code though so am not sure how the datarow will handle the > itemarray if any of its copied contents do not match the columns > collection in the parent table. > The items in a datarow have forever to be confirm the datacolumn colletion in a datatable. Although that your first solution can be the solution will than the "importrow" methode probably be better because that keeps the rowstate, while yours is setting that as added. Cor Bonzol,
Your select is impossible with a Option Strict on. Therefore it can give unpredictiable results depending on the values that are in your variables. Therefore I doubt if it is wise to start with trying to use methods as merge, importrow etc, if you have not solved this before and know what you are doing. Just my thought, Cor Show quoteHide quote "Bonzol" <Bon***@hotmail.com> schreef in bericht news:1153290930.522145.304110@b28g2000cwb.googlegroups.com... > tables were previously created via > > > Public Function jWildCard(ByVal table1 As String, ByVal returncolumn > As String, ByVal table2 As String, ByVal checkcolumn As String, ByVal > table1joinField As String, ByVal table2joinField As String, ByVal > checkvalue As String) As DataTable > ' > Dim StringToReturn As String > > StringToReturn = "" > Dim SQL As String > SQL = "SELECT " + table1 + "." + returncolumn + " FROM " + > table1 + " INNER JOIN " + table2 + " ON " + table1 + "." + > table1joinField + " = " + table2 + "." + table2joinField + " WHERE(" + > table2 + "." + checkcolumn + " LIKE '%" + checkvalue + "%')" > Dim dataAdapter As System.Data.OleDb.OleDbDataAdapter > dataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQL, > Me.OleDbConnection1) > > Dim dt As System.Data.DataTable > dt = New System.Data.DataTable > dataAdapter.Fill(dt) > > > Return dt > > > this is done twice and both tables have the same Select table and > return coloumn. > Bonzol,
One of the things you have to know when working with datarows and datatables is that you never can have a datarow in more than one table. Simple because of the fact that the datarow has a property that references to the table it is in. However you can use copies of datarow. But first answer the question from Steven. Cor Show quoteHide quote "Bonzol" <Bon***@hotmail.com> schreef in bericht news:1153288677.442118.315240@s13g2000cwa.googlegroups.com... > Hey there, just starting out using data tables, can anyone tell me how > to combine 2 tables? > > Ive tried just coyping the info from 1 table to another, which both > have exactly the same table layout > data is a datatable > data1 is a datatable > > data.Rows.Add(data1.Rows(0).Item(0)) > > but it dosnt work, can anyone tall me what im doing wrong? >
HELP! Why is SqlException being caught in this code if it can't connect to SQL Server?
Public Shared Property Barcode scanning Display Unicode characters on Winforms concurrency question Refreshing datagridview after a rowtemplate height change String comparison algorithms ADO.NET in VB6 How to download a file from Server to client local pc without user intervention? ByRef - delayed update of the original variable? |
|||||||||||||||||||||||