Mapping corresponding columns - again !!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have two tables with identical columns(same names,data types and constraints) in two different databases.
Say c1,c2,c3.....c90.
The order of the columns are different in the two tables.
Say c1,c2,c3,....c90 in source table and
say c5,c6,c9,.....c90 in the destination table.

Without knowing beforehand as to what the order of columns are in the destination table,
how do I take a record out of source table and insert it in destination table ?

I am able to successfully do it if the columns of both the
tables are in the same order.(two dataadapters and performing an update as suggested by Miha).

I had posted a similar query before.Miha had suggested that column order doesn't matter at all.
Parameters are mapped to columns normally.
( IDataParameter.SourceColumn property).

I did not understand what it meant.Does it mean that while doing the update, the corresponding column names are automatically mapped ? I tried and and I am getting errors.
Could anyone help out ?
 
Hi Julian,

I once have made this,

Have a look what it can do for you.

I hope this helps,

Cor
\\
For i As Integer = 0 To ds1.Tables(0).Rows.Count - 1
Dim swN As Boolean = True
For y As Integer = 0 To ds2.Tables(0).Rows.Count - 1
Dim a As Integer = ds2.Tables(0).Rows.Count - 1
If CInt(ds1.Tables(0).Rows(i).Item(0)) = _
CInt(ds2.Tables(0).Rows(y).Item(0)) Then
ds2.Tables(0).Rows(y).Item(1) = ds1.Tables(0).Rows(i).Item(1)
swN = False
Exit For
End If
Next
If swN Then
Dim dr As DataRow
dr = ds2.Tables(0).NewRow
For y As Integer = 0 To
ds1.Tables(0).Rows(i).ItemArray.Length - 1
dr(y) = ds1.Tables(0).Rows(i).ItemArray(y)
Next
ds2.Tables(0).Rows.Add(dr)
End If
Next
///
 
Thank You Cor. Had a few questions

1. If CInt(ds1.Tables(0).Rows(i).Item(0)) = CInt(ds2.Tables(0).Rows(y).Item(0)) The
Is this comparing the column names of the two tables ? Why should we cast as CInt

2. If swN The
I believe this is the case if the column names are not equal.Right

3. I don't understand what you are doing if swN is true. We should be doing a comparison to find out the matching columns, right

Thank you for the help again

Julia
 
The datatables have only 2 columns, 1 key an integer value and the other a
string value.

What it has to do is that when a key does not exist in table 2 it is added
as a row from table 1 in that table and else will the value of table 1
replace the value in table2.
1. If CInt(ds1.Tables(0).Rows(i).Item(0)) =
CInt(ds2.Tables(0).Rows(y).Item(0)) Then
Is this comparing the column names of the two tables ? Why should we cast
as CInt ?

This is the keycolumn that is tested if it is an equal row. In this case an
integer column
2. If swN Then
I believe this is the case if the column names are not equal.Right ?

The keys are not equal the key from file 1 does not exist in 2.
3. I don't understand what you are doing if swN is true. We should be
doing a comparison to find out the matching columns, right ?I transport in one take the itemarray from a datarow from table1 to the new
datarow from table2.

It transports only from dataset1 to dataset 2 and not visa versa, I hope
that this was something what you was looking for.

However keep in mind, it is just a part of a sample and there has not much
testing been about it. With my sample values it did work as far as I could
see then.

Cor
 
Back
Top