---------------
Hi Sahil,
your post is really good, however i have a few queries.....
1)In the below code block, does the ADO internals work in an inefficient
way.. i.e
first of all the inserted records will be inserted by calling the insert
stored procs(as per teh command object)......
and then for the same inserted rows which had a couple of fields updated
an
update stored proc will be run? Is this not inefficient work by the ADO by
making an extra call to the db for the updates?
2)In the below code example, does the connection has to be the same for
both
the master and transaction tables?
Can just the idbtransaction be used to be passed around?
This is because the way my classes and methods are defined, the connection
woulld be pooled and the updates for the master and transaction tables
from
the UI may be in a separate state and the code is modular so that the
connection object obtained may differ at each time and transaction based
details are kept transparent from the data layer (besides the information
about the transaction through IDBtransaction).
I hope i was clear to explain my queries..
Appreciate your response on the same.
Thanks.
:
Okay now that the problem is in my head.
The approach really doesn't change much even in this case, but it does
depend on your final database structure.
So if Group were to be saved in a table, and groupdetails in another,
you'd
have to call ExecuteNonQuery instead of DataAdapter.Update for Group,
and
Datadapter.Update(DataRow[]) for the GroupDetails table.
The basic concept remains the same.
Group Insert
Group Update
GroupDetail Insert
GroupDetail Update
... Just remember, contrary to MSDN docs, DataAdapter.Update(dataSet)
doesn't
really call the commands on EACH row in the dataset. Only in the first
datatable, or the datatable identified by the name "Table".
To wrap it all up in a transaction,
Do a
connection.open
tran = conn.BeginTransaction
cmd.transaction = tran
cmd.executenonquery
cmd2.transaction = tran
datadapter.insertcommand = cmd2
datarow[] insertedrows =
groupdetailsdatatable.select("","",DataRowState.Added)
dataadapter.Update(insertedrows);
/// other such commands
tran.commit
conn.close
I wish I could hand you a copy of my next book with a working example of
this in Chapter #10, but that doesn't come till September. I know it
isn't
quite as straightforward as you'd like it to, but you do have to go
through
all the hoops.
Did I mention one more detail - If a new Group is inserted, then you
willl
have to retreive the right key values to put in GroupDetails. Now
inserting
a Group can be into one table, or sixty tables - any SQL will do, but if
there is a foreign key association, you will definitely need to do that.
The EXACT answer depends on your DB structure. But the concepts are
still
the same.
- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/
That's it. I'm sorry if wasn't very clear the first time.
Thanks,
Glen