Reza.Hoque
Reza.Hoque

Reputation: 2750

copy data from one table to another table in another database

I have to write a program that will copy data from one table to a similar table in a different database. This part is fine but I also have some additional requirements.

It has to be row by row. and after copying one row, i have to somehow make sure/confirm that the row is copied successfully. And after that i need to delete that row from the source table in the source database and step into the next row.

Now my question is how would I confirm that the row is copied successfully? Is there any good way to do that?

Upvotes: 1

Views: 1359

Answers (4)

JotaBe
JotaBe

Reputation: 39055

You need to do all the operations in the same transaction to warranty that the data in the source table is only, and always, deleted when it has been copied to the second table.

For doing this, you can use a TransactionScope, which look like this:

using(var ts = New TransactionScope()) { // Do operations here: read source, write copy, delete destination ts.Complete(); }

See the docs for TransactionScope here. And take into account these two things:

  1. you need to add a reference to System.Transactions assembly in your project
  2. you need to start the MSDTC service (Distributed Transaction Coordinator).

How it works: if there are no erros in any of the operations (read source, write copy, deelte source) then the tx.Complete(); line is executed, which means that all the operations are confirmed. If there are any error in any of the operations, when the error happens, the program continues outside of the using block, thus the ts.Complete() is not called, and all the operations which had been done are rolled back.

In general, if you make any DB operation using any technology (classical SqlCommand, EF or whatever), if it doesn't throw an exception it means that the operation went fine. So you can do something similar to this using try-catch-finally but it can fail on some occassions, for example: what if the program breaks for any unexpected reason? With the TransactionScope, the pending operation will be rolled backed, but with other solutions, probably not.

Upvotes: 1

Prince Sharma
Prince Sharma

Reputation: 118

You can do so by row count method . First create an instance of Dataset . It is there in using System.Data; .

    DataSet ds = new DataSet();

And

    ds = rpt3.getShowAll();
    int x = ds.Tables[0].Rows.Count;

rpt3 is my class in which getShowAll() is present.Here my method getShowAll() has a procedure that is having the complete table to be copied.Now
write the code for the copying function . Now again count the rows for the next table and compare them .(Note you have to fetch again while counting rows again means

     ds = rpt3.getShowAll();

will be used again .

Upvotes: 0

PhillipH
PhillipH

Reputation: 6222

If its the same database a simple database transaction will guarantee that the Copy, Confirm, Delete set is executed as a batch. In normal circumstances the SELECT COUNT check would be unneccessary - if your insert fails, your RDBMS will tell you about it by throwing an exception; however to answer your question -

Psudocode:

foreach(@ID in listOfIDs)
{
   BEGIN TRANSACTION
   INSERT INTO DESTINATIONTABLE SELECT * FROM SOURCETABLE WHERE ID=@ID
   DECLARE @COUNTSUCCESS INT
   SET @COUNTSUCCESS = (SELECT COUNT(1) FROM DESTINATIONTABLE WHERE ID=@ID)
   IF(@COUNTSUCESS <> 1) 
   BEGIN
      // Throw some kind of exception
      ROLLBACK TRANSACTION
   END
   DELETE FROM SOURCETABLE WHERE ID=@ID
   COMMIT TRANSACTION
}

Upvotes: 0

Alexander Gelbukh
Alexander Gelbukh

Reputation: 2240

First, if the row is not copied, the command with which you insert it should return an error. If it didn't, then the row was copied correctly.

But if you want to be sure, then before inserting the new row to the second database, query the second database with a SELECT WHERE all the values of all columns are exactly as in the current row. Remember the number of rows returned.

Then copy the new row, and make the same query again. Now the number of rows returned should be greater by one.

If there cannot be equal rows, then you will probably want that before the copy the number of rows returned be 0 and after, be 1.

Upvotes: 0

Related Questions