I’ve added to my project a dataset item called : myDataSet.xsd
After this, I drop tables on myDataSet.xsd file. That’s all okey.
What I want to do is, to build Sql Transaction’s as function on
that dataset, how can I do this?
I’ve added to my project a dataset item called : myDataSet.xsd After this, I
Share
A
DataSetis an in-memory structure – there is no direct relationship to SQL orSqlTransaction.I think what you are probably referring to is performing operations on the generated
TableAdapterinstances using a SQL transaction.It’s tricky.
Sahil Malik has a reasonably complete primer on the subject (yes, it’s really a “subject”). Essentially what it all boils down to is extending the
TableAdaptervia a partial class with aBeginTransactionmethod which opens the innerSqlConnectionand holds it open and hands you back aSqlTransactionwhich you then use normally. It’s cumbersome, and you have to do this for everyTableAdapter. Alternatively, you can useSystem.Transactions.TransactionScope, but you have to jump through various hoops in order to avoid a promotion to DTC (distributed transaction).I’d encourage you to take a look at the linked article, rather than me copying and pasting the code in here.
If you have a lot of
TableAdapters, or in general need to combine more than one of them in a single transaction, Sahil’s approach isn’t going to scale too well. In the past (when I still usedTableAdapters) I used a method similar to Ryan Whitaker’s, which is basically monkey-patching; it uses Reflection for most of the heavy lifting.Those two are kind of the definitive resources on making
TableAdaptercode transactionally-safe. Personally, I just wouldn’t use a typedDataSetat all any more; it’s all but obsolete now that Linq to SQL and EF are around, they are much more powerful, have deferred execution, don’t rely exclusively on in-memory structures, and use a Table Data Gateway + Unit of Work pattern that is very easy to get working with transactions. In my experience, if those do not suffice, usually neither will aDataSetand I have to drop down to bareSqlCommandinstances. But if for some reason you absolutely must use aDataSetfor data access, one of the two aforementioned methods should be what you need.