TableAdapters and transactions

  • Thread starter Thread starter TheMaxx
  • Start date Start date
I wouldn't recommend reflection because you'll need full privileges to do
that. Instead you have at least two options:
1. avoid table adapters and do coding by yourself
2. expose connection by creating a partial class
 
To use transactions with table adapters you can use TransactionScope:

using (TransactionScope scope = new
TransactionScope(TransactionScopeOption.RequiresNew))
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
try
{
// Put code here
scope.Complete(); // Call this to notify that a transaction is
finished
}
catch (Exception ex)
{
// Exception code here
}
}
}

If scope.Complete() is not called the transaction will back out.
Note how the TransactionScope is called prior to the "new SqlConnection".
Hope this he;ps.
 
1. TableAdapters save A LOT of work (creating Inser/Update/delete commnds
for each table in my DB), please tell me : Do you create those methods
manually?
2. and do this for each TableAdapter that needs to be updated with
transaction) souds too much additional work to add on generated code.
This all sound like walkarounds, what did Microsoft have in mind for this
kind of situatisons?



Miha Markic said:
I wouldn't recommend reflection because you'll need full privileges to do
that. Instead you have at least two options:
1. avoid table adapters and do coding by yourself
2. expose connection by creating a partial class

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

TheMaxx said:
How to work with tableAdapters and transactions?
I see no Connection or Transaction property in TableAdapters.
This guy is doing something with reflection:
http://mybug.bloger.hr/post/tableadapters-and-transactions/179426.aspx

Isn't there a better way, something like best practise?
 
I can't speak for others, but I create those methods manually. I am
actually working on a routine that creates strongly typed datasets from my
stored procedures, and doesn't create the table adapters. I'll use stored
procedures for the updates, and write my own code to call them.

You only have to write it once (unless something changes), and you have
more control over it.

A lot of people use the TableAdapters, though.

Robin S.
-----------------------------
TheMaxx said:
1. TableAdapters save A LOT of work (creating Inser/Update/delete commnds
for each table in my DB), please tell me : Do you create those methods
manually?
2. and do this for each TableAdapter that needs to be updated with
transaction) souds too much additional work to add on generated code.
This all sound like walkarounds, what did Microsoft have in mind for this
kind of situatisons?



Miha Markic said:
I wouldn't recommend reflection because you'll need full privileges to do
that. Instead you have at least two options:
1. avoid table adapters and do coding by yourself
2. expose connection by creating a partial class

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

TheMaxx said:
How to work with tableAdapters and transactions?
I see no Connection or Transaction property in TableAdapters.
This guy is doing something with reflection:
http://mybug.bloger.hr/post/tableadapters-and-transactions/179426.aspx

Isn't there a better way, something like best practise?
 
Back
Top