0

Situation: a third party drops an XML file in a location, daily, where my windows app pick it up, drop the data from a table in my DB and replace it by the new data. Basically the XML represent the entire data not only the changes.

Question: instead of dropping the table every day, I would like to update my DB with only the updated data or new data, without going through every single row and compare them. Is there a way to do it in C#?

Thank you.

6
  • If you don't compare the new data with the existing data then how will you know what to update? Commented Feb 13, 2019 at 20:39
  • 1
    There are many ways to do it so the simple answer is yes. Can you show us what you have tried so far. SO is not a code writing service but is here to help and support with problems. Commented Feb 13, 2019 at 20:41
  • True, that's why I was wondering if there is a less cumbersome way. Commented Feb 13, 2019 at 20:41
  • sorry I don't have access to the code at the moment, but basically I just read the XML, deserialize it, drop my table and loop through my xml object and add each entry to the empty table Commented Feb 13, 2019 at 20:44
  • Use merge statement This will help you Commented Feb 13, 2019 at 20:45

1 Answer 1

1

You don't have to drop the table. You can also truncate it, or delete all rows from it.

Dropping and truncating are fast operations but they actually change the data model (DDL statement) and are quite blunt. They basically throw away whatever data the database has for a table.

Deleting is an DML operation (modifying the data without touching the database structure). It has the potential benefit of being transacted, so you may rollback to the previous data if the import fails for whatever reason. But it may take longer because the database will keep a record of the changes made.

The statements are simple enough:

truncate TableName; 

or

delete * from TableName; 

If you really want to compare changes, you have to query the data first, or write a more complicated upsert statement, for which the exact solution may differ based on the database you're using. I doubt if that will be faster or more efficient, and I'd go for truncate or delete.

Sign up to request clarification or add additional context in comments.

1 Comment

I will try a solution with upsert and see where it could lead. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.