I will explain firstly what I do, then I will specify where is the problem.
My application gets an XML file from an authenticated user through (file uploader), then I map (I mean migrate) the data stored in the XML file to its equivalent one in my database.
I get the data from the XML file through LINQ.
-
My first question
Each element in
the XML file has an equivalent entity
in my database. What is the best and
more performant way to insert
more than one record in a
specific table and guarantee that if
there is something wrong in the data, rollback the whole operation?
Is there some example of how to do
this? Do you have any suggestions concerning
validating the XML data? -
My second question:
In the first question, I talk about the
the (INSERT) operation. If the
user changes some data in the XML
file then I want to update my
database with the new data. How should I
do this? Should I compare each
record, or try to insert and, if that fails,
then update this record?
The simple answer here is: use a transaction. The point of transactions is to provide you with a mechanism whereby you can execute multiple commands, then either commit them as a single unit of work or roll them back completely so that the database is left in a state as if your operations had never taken place.
The try-error-different retry pattern is not a desirable one if it can be easily avoided. Your SQL should either use a statement that is designed to conditionally insert or update depending on existing data (such as the SQL Server
MERGEcommand; I don’t have Informix experience so I can’t speak to what it supports or ifMERGEis ANSI SQL), or you should do this conditional logic yourself within the SQL.