Can transaction failure event handler be implemented on a Web-based app?
Can transaction failure event handler be implemented on a Web-based application in which either records will be inserted or updated in about 10 tables?
To handle transactions you have to use ADO.NET features. It's not a matter of Web-based or Windows-based applications. ADO.NET provides a BeginTransaction() method for all objects implementing IDbConnection (such as OleDbConnection and SqlConnection). This method returns an IDbTransaction object you can use to commit or rollback an entire operation. The typical usage pattern is:
IDbConnection cn = new SqlConnection("your connection string"); IDbTransaction trans = cn.BeginTransaction(); try { //try to update/insert everything using cn as the connection //Use cn.CreateCommand() to get IDbCommand to execute queries. trans.Commit(); } catch { //cancel trans.Rollback(); } finally { cn.Close(); }
Using the ADO.NET interfaces instead of the SqlClient classes directly, makes it far easier to port this code later to another DB engine. In the case shown above, only the first line that creates the new SqlConnection would need to be changed. Better yet, you could use an abstract factory so you have a single point of change in the future.
Dig Deeper on C# programming language
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.
Meet all of our Microsoft .Net Development experts
Start the conversation
0 comments