15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Traditional Transactions ❘ 609<br />

ADO.<strong>NET</strong> is covered in detail in Chapter 30, “Core ADO.<strong>NET</strong>.”<br />

TradiTional TransaCTions<br />

Before System.Transaction was released, you could create transactions directly with ADO.<strong>NET</strong>, or you<br />

could do transactions with the help of components, attributes, <strong>and</strong> the COM+ runtime, which is covered<br />

in the namespace System.EnterpriseServices . To show you how the new transaction model compares to<br />

the traditional ways of working with transactions, we present a short look at how ADO.<strong>NET</strong> transactions<br />

<strong>and</strong> transactions with Enterprise Services are done.<br />

ado.neT Transactions<br />

Let ’ s start with traditional ADO.<strong>NET</strong> transactions. If you don ’ t create transactions manually, there is<br />

a single transaction with every SQL statement. If multiple statements need to participate with the same<br />

transaction, however, you must create a transaction manually to achieve this.<br />

The following code segment shows how to work with ADO.<strong>NET</strong> transactions. The SqlConnection<br />

class defi nes the method BeginTransaction() , which returns an object of type SqlTransaction . This<br />

transaction object must then be associated with every comm<strong>and</strong> that participates with the transaction.<br />

To associate a comm<strong>and</strong> with a transaction, set the Transaction property of the SqlComm<strong>and</strong> class<br />

to the SqlTransaction instance. For the transaction to be successful, you must invoke the Commit()<br />

method of the SqlTransaction object. If there is an error, you have to invoke the Rollback() method,<br />

<strong>and</strong> every change is undone. You can check for an error with the help of a try / catch <strong>and</strong> do the rollback<br />

inside the catch:<br />

using System;<br />

using System.Data.SqlClient;<br />

using System.Diagnostics;<br />

namespace Wrox.ProCSharp.Transactions<br />

{<br />

public class CourseData<br />

{<br />

public void AddCourse(Course course)<br />

{<br />

var connection = new SqlConnection(<br />

Properties.Settings.Default.CourseManagementConnectionString);<br />

SqlComm<strong>and</strong> courseComm<strong>and</strong> = connection.CreateComm<strong>and</strong>();<br />

courseComm<strong>and</strong>.Comm<strong>and</strong>Text =<br />

"INSERT INTO Courses (Number, Title) VALUES (@Number, @Title)";<br />

connection.Open();<br />

SqlTransaction tx = connection.BeginTransaction();<br />

try<br />

{<br />

courseComm<strong>and</strong>.Transaction = tx;<br />

courseComm<strong>and</strong>.Parameters.AddWithValue("@Number", course.Number);<br />

courseComm<strong>and</strong>.Parameters.AddWithValue("@Title", course.Title);<br />

courseComm<strong>and</strong>.ExecuteNonQuery();<br />

tx.Commit();<br />

}<br />

catch (Exception ex)<br />

{<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!