473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ADO.NET rollbacks

If the transaction object is created using a finally statement, will
the transaction automatically be rolled back if an error is thrown, or
do i need to explicitly code the rollback. The code that I am thinking
about using (without an explicit rollback) is below:

Greg.
^^^^^^^^^^^^^^^^^^^^^^^^^BEGIN CODE
SNIP^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
using (SqlConnection conn = new SqlConnection(_connectionString))
{

conn.Open();
using (SqlTransaction transaction =
conn.BeginTransaction(IsolationLevel.Snapshot))
{
//Delete existing rows
using (SqlCommand cmdDelete = new SqlCommand())
{
cmdDelete.Transaction = transaction;
cmdDelete.Connection = conn;
cmdDelete.CommandType =
CommandType.StoredProcedure;
cmdDelete.CommandText = "sp_deletedstdate";
cmdDelete.Parameters.AddWithValue("@timeZone",
Int32.Parse(uxTimeZones.SelectedValue));

cmdDelete.ExecuteNonQuery();
throw new Exception();
}

//Insert rows from grid
using (SqlCommand cmdInsert = new SqlCommand())
{
cmdInsert.Connection = conn;
cmdInsert.Transaction = transaction;
cmdInsert.CommandType =
CommandType.StoredProcedure;
cmdInsert.CommandText = "sp_insertdstdate";
foreach (GridViewRow row in uxDstGrid.Rows)
{
DateTime from =
((BasicFrame.WebControls.BDPLite)row.FindControl(" uxDstStart")).SelectedDate;
DateTime to =
((BasicFrame.WebControls.BDPLite)row.FindControl(" uxDstEnd")).SelectedDate;
int offSet =
Int32.Parse(((DropDownList)row.FindControl("uxDstO ffset")).SelectedValue);
cmdInsert.Parameters.Clear();
cmdInsert.Parameters.AddWithValue("@timeZone",
Int32.Parse(uxTimeZones.SelectedValue));
cmdInsert.Parameters.AddWithValue("@start",
from);
cmdInsert.Parameters.AddWithValue("@end", to);
cmdInsert.Parameters.AddWithValue("@offset",
offSet);

cmdInsert.ExecuteNonQuery();
}
}
//No errors, so commit
transaction.Commit();
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^END CODE
SNIP^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^

Jul 27 '07 #1
2 1400
Greg,

There is nothing here in a finally statement (although semantically,
there is, given that the using statements compile to try/finally blocks).

In this case though, if any exception is thrown, your transaction will
not commit.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg" <sp**********@yahoo.co.ukwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
If the transaction object is created using a finally statement, will
the transaction automatically be rolled back if an error is thrown, or
do i need to explicitly code the rollback. The code that I am thinking
about using (without an explicit rollback) is below:

Greg.
^^^^^^^^^^^^^^^^^^^^^^^^^BEGIN CODE
SNIP^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
using (SqlConnection conn = new SqlConnection(_connectionString))
{

conn.Open();
using (SqlTransaction transaction =
conn.BeginTransaction(IsolationLevel.Snapshot))
{
//Delete existing rows
using (SqlCommand cmdDelete = new SqlCommand())
{
cmdDelete.Transaction = transaction;
cmdDelete.Connection = conn;
cmdDelete.CommandType =
CommandType.StoredProcedure;
cmdDelete.CommandText = "sp_deletedstdate";
cmdDelete.Parameters.AddWithValue("@timeZone",
Int32.Parse(uxTimeZones.SelectedValue));

cmdDelete.ExecuteNonQuery();
throw new Exception();
}

//Insert rows from grid
using (SqlCommand cmdInsert = new SqlCommand())
{
cmdInsert.Connection = conn;
cmdInsert.Transaction = transaction;
cmdInsert.CommandType =
CommandType.StoredProcedure;
cmdInsert.CommandText = "sp_insertdstdate";
foreach (GridViewRow row in uxDstGrid.Rows)
{
DateTime from =
((BasicFrame.WebControls.BDPLite)row.FindControl(" uxDstStart")).SelectedDate;
DateTime to =
((BasicFrame.WebControls.BDPLite)row.FindControl(" uxDstEnd")).SelectedDate;
int offSet =
Int32.Parse(((DropDownList)row.FindControl("uxDstO ffset")).SelectedValue);
cmdInsert.Parameters.Clear();
cmdInsert.Parameters.AddWithValue("@timeZone",
Int32.Parse(uxTimeZones.SelectedValue));
cmdInsert.Parameters.AddWithValue("@start",
from);
cmdInsert.Parameters.AddWithValue("@end", to);
cmdInsert.Parameters.AddWithValue("@offset",
offSet);

cmdInsert.ExecuteNonQuery();
}
}
//No errors, so commit
transaction.Commit();
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^END CODE
SNIP^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^

Jul 27 '07 #2
That's great, thanks a lot Nicholas.

Jul 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
6
by: George McLean | last post by:
Hello, I am trying to isolate some performance issues. The database is DB2 v8.1 running on a Win2000 server with 4 processors and 1gb of RAM. The application is a GIS application that uses...
2
by: Tim.D | last post by:
Hello people, Thanks to Serge, PM, Knut and a host of others I have now successfully completed my very first SQL Stored Procedure. Takes some 13mins or so to run, but that was expected as it...
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
5
by: renny | last post by:
Dear Oracle/DB2 DBAs, I'm looking for advice on tuning Oracle 9i and DB2 8.1 for rollbacks. I have a number of developers, who each build and test our client/server database application...
0
by: Jim Heavey | last post by:
I have tested a procedure in TOAD and it functions as expected, meaning for this particular transaction it returns and error message and performs a rollback within the procedure. When I call...
1
by: boston01 | last post by:
I am testing a database application. It works fine when only one user uses the application, however, when there are concurrent users, I saw rollbacks in event log. Can experts in this group...
46
by: dunleav1 | last post by:
I have a process that does inserts that runs 50% slower that Oracle and Mssql. Queries normally take 50% slower than normal. DB2, Oracle, Mssql all are configured on same os, same disk array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.