[dba-VB] Copy tables

Gustav Brock Gustav at cactus.dk
Wed Feb 6 07:30:02 CST 2008


Hi Stuart et al

Oh my, make a note on this should you ever wish to copy data between DataSets or DataTables with method ImportRow.

The secret is the value of property RowState of the individual rows. Rows read from a table have a RowState of Unchanged.
If a row is copied to another DataSet/DataTable by method ImportRow, RowState remains Unchanged.

In my code sample below, I marked rows as Modified with method SetModified.

The Update method of the DataTableAdapter, which is called to write the data rows to the underlying table, is quite clever as it doesn't touch rows with RowState Unchanged.
However, rows with RowState Modified will only be used to update existing records in the target table, thus it will not attempt to append or insert any such row. 

For a row to be inserted by the later Update call, it has to have RowState set to Added.

So, for my code below, I have to decide if a row should update an existing record, or if it should be appended, or if it should be ignored - and then call the method SetModified or SetAdded or nothing:

                    // Mark row as modified.
                    kundeRowS.SetModified();
or:
                    // Mark row as new.
                    kundeRowS.SetAdded();

When I changed the code from SetModified to SetAdded and replaced the Fill method with:

      kundeAdapterT.Update(kunderT);

all records were written to the target table.

Of course, if you can write your code to add rows not using method ImportRow - with LoadDataRow or like:

                    kunderT.Rows.Add(rowToAdd);

RowState of those rows, I guess, will be switched to Added automatically.

/gustav


>>> Gustav at cactus.dk 05-02-2008 18:02 >>>
Hi Stuart et al

No there is no GUI, just code.
I have some code like this. It add rows but will not save them to the database table:

                VrsSourceTableAdapters.DataTableKundeTableAdapter kundeAdapterS =
                    new VrsSourceTableAdapters.DataTableKundeTableAdapter();
                VrsSource.DataTableKundeDataTable kunderS;

                VrsTargetTableAdapters.DataTableKundeTableAdapter kundeAdapterT =
                    new VrsTargetTableAdapters.DataTableKundeTableAdapter();
                VrsTarget.DataTableKundeDataTable kunderT;

                kunderS = kundeAdapterS.GetDataVrsKunder();
                kunderT = kundeAdapterT.GetDataVrsKunder();

                foreach (VrsSource.DataTableKundeRow kundeRowS in kunderS)
                {
                    Console.WriteLine("Kunde: " + kundeRowS.navn + "");
                    Console.WriteLine("  Kundenr: " + kundeRowS.kundeNr + "");
                    // Mark row as modified.
                    kundeRowS.SetModified();
                    kunderT.ImportRow(kundeRowS);
                }
 
                Console.WriteLine("s: " + kunderS.Count.ToString());
                Console.WriteLine("t: " + kunderT.Count.ToString());

                kundeAdapterT.FillVrsKunder(kunderT);

Target table is residing in MSDE. 
Count of s returns the count of records to add. 
Count of t returns existing records + count of records to add.

What am I missing?

/gustav


>>> stuart at lexacorp.com.pg 05-02-2008 00:06:55 >>>
Do you mean have the user physically browse through the rows looking for specific records?

If that is the case, I would probably go with a DatagridView bound to the source data.
When the user selects a row (double click, highlight and click a button or whatever), read the 
row data and feed it to a TableAdapter.Insert on the target table.


On 4 Feb 2008 at 18:41, Gustav Brock wrote:

> Hi all
> 
> I have created two tableadapters, a source and a target, and need to
> browse through the source looking for rows and, when a row meeting
> some conditions is found, copy this to the target. What is the best
> method? 
> 
> /gustav





More information about the dba-VB mailing list