Hi, I'm tyring to use .net transaction, I have code like this :
SqlConnection connection = Company.Instance.NewConnection();
using (connection) {
SqlTransaction transaction = connection.BeginTransaction();
try {
string stringSQL = GetSQLString();
SqlCommand command = new SqlCommand(stringSQL, connection,
transaction);
command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception) {
transaction.Rollback();
throw;
}
}
How I understand when stringSQL is invalid transaction will rollback
automatically, am I right ? Than, when we going to catch, RollBack()
will thorow an error, am I right ? 3 1644
This is a simple enough code piece to try out for yourself to determine
whether or not it will happen. I'm not sure that Rollback will throw if the
transaction is already aborted. However, if it does, you can easily wrap
that call in a try/catch block.
However, if you are using .NET 2.0, you might want to consider using the
System.Transactions namespace. Transaction management can be a tricky thing
to do if you have multiple objects/multiple resources, and it's better to
let this stuff run in the context of a transaction manager. If you aren't
using .NET 2.0, I would seriously consider using COM+ to provide this for
you.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<me******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com... Hi, I'm tyring to use .net transaction, I have code like this : SqlConnection connection = Company.Instance.NewConnection(); using (connection) { SqlTransaction transaction = connection.BeginTransaction(); try { string stringSQL = GetSQLString(); SqlCommand command = new SqlCommand(stringSQL, connection, transaction); command.ExecuteNonQuery(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } How I understand when stringSQL is invalid transaction will rollback automatically, am I right ? Than, when we going to catch, RollBack() will thorow an error, am I right ?
<me******@gmail.com> wrote: Hi, I'm tyring to use .net transaction, I have code like this : SqlConnection connection = Company.Instance.NewConnection(); using (connection) { SqlTransaction transaction = connection.BeginTransaction(); try { string stringSQL = GetSQLString(); SqlCommand command = new SqlCommand(stringSQL, connection, transaction); command.ExecuteNonQuery(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } How I understand when stringSQL is invalid transaction will rollback automatically, am I right ? Than, when we going to catch, RollBack() will thorow an error, am I right ?
A better way of working, IMO, is to do:
using (SqlTransaction transaction = connection.BeginTransaction())
{
using (SqlCommand command = new SqlCommand(stringSQL, connection,
transaction))
{
command.ExecuteNonQuery();
}
transaction.Commit();
}
The transaction will automatically roll back if it is disposed without
being committed.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] wrote: <me******@gmail.com> wrote:
Hi, I'm tyring to use .net transaction, I have code like this : SqlConnection connection = Company.Instance.NewConnection(); using (connection) { SqlTransaction transaction = connection.BeginTransaction(); try { string stringSQL = GetSQLString(); SqlCommand command = new SqlCommand(stringSQL, connection, transaction); command.ExecuteNonQuery(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } How I understand when stringSQL is invalid transaction will rollback automatically, am I right ? Than, when we going to catch, RollBack() will thorow an error, am I right ?
A better way of working, IMO, is to do:
using (SqlTransaction transaction = connection.BeginTransaction()) { using (SqlCommand command = new SqlCommand(stringSQL, connection, transaction)) { command.ExecuteNonQuery(); } transaction.Commit(); }
The transaction will automatically roll back if it is disposed without being committed.
Didn't know that... Really elegant way. Bless Anders for "using()" ;-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christopher J. Bottaro |
last post by:
Hi,
Why is there no support for explicit transactions in the DB API? I mean
like transaction() to start the trans and commit() and rollback() would end
the trans or something.
The reason why I...
|
by: Richard Maher |
last post by:
Hi,
I am seeking the help of volunteers to test some software that I've
developed which facilitates distributed two-phase commit transactions,
encompassing any resource manager (e.g. SQL/Server...
|
by: Iain Mcleod |
last post by:
I wish to do a series of inserts on a sql server database in the context of
a transaction. The inserts will be done as a series of stored procedure
calls. I wish to be able to rollback any...
|
by: Terri |
last post by:
I have a table called Transactions with 3 fields: ID, Date, and Amount. Each
ID can have multiple transactions in one particular year. An ID might not
have had any transactions in recent years.
...
|
by: TD |
last post by:
I am trying to add transactions to my code. The original code worked
fine until I followed an example to setup transactions, now the code
does strange things, but no error messages.
Could...
|
by: Mike P |
last post by:
I've been using C# transactions for a while and had no problems with
them. Using try catch blocks I can trap basically all possible errors
and rollback all necessary data.
Over the last few...
|
by: mark |
last post by:
In Java, you can use JBoss or similar to host EJB that will manage your
transactions for you. You could, of course, write your own transactions
using JDBC.
In .NET, we can specify our own...
|
by: radcaesar |
last post by:
Customer Table
ID Name Address City Phone
1 Vijay Stores 6,Gandhi Road Pondy 0413-276564
2 Ram Stores 3, MG Road, Pondicherry 0413-29543756
3 Balu Papers 3, RG...
|
by: Sridhar |
last post by:
Hi,
I am trying to implement sql transactions. But I am not knowing how to
do that. I created a data access layer which contains methods to
select/insert/update tables in a database. I have also...
|
by: Rami |
last post by:
I have some requirement for an automated payment system. The system has
four machines setup as follows:
1- Two machines have a clustered database.
2- Two machines have a .net business logic...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |