ARTICLE

Transaction in .NET

Posted by Rahul Kumar Saxena Articles | Visual Basic 2010 September 12, 2007
This article shows how we can achieve successful transaction maintaining consistency of records in the database.
Download Files:
 
Reader Level:

Transaction:

 

Transaction is a process. A transaction can be a set of more then one process. If a transaction is a set of more then one process, then for successful completion of that transaction, it is necessary that all processes should be executed successfully in that transaction.

Let understand the transaction concept with this example.

Here in this example, I am using a set of two-process transaction. Here I am executing two commands simultaneously with transaction. With first command I am deleting a record from table and with second I am inserting a new record in the table.

 

Before executing the transaction, the record in the table is:

Figure 1: Here in transaction I am deleting the record where location is England and also I am inserting a record in the table.

The code for this transaction is:

Imports System

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Text

Imports System.Windows.Forms

Imports System.Data.SqlClient

 

Namespace TranSactionIn.NET

    Partial Public Class Form1

        Inherits Form

 

        Public Sub New()

            InitializeComponent()

        End Sub

        Private con As SqlConnection

        Private cmd1 As SqlCommand = New SqlCommand()

        Private cmd2 As SqlCommand = New SqlCommand()

        Private trm As SqlTransaction

 

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 

            con = New SqlConnection("Data Source=(local);Initial Catalog=Employee; Uid=sa; pwd=")

            cmd1.CommandText = "delete from EmployeeRecord where Location='England'"

            cmd2.CommandText = "Insert into EmployeeRecord (DeptNo,Name,DepartName,Location) values ('901','Sahib','Manager','Africa')"

            cmd1.Connection = con

            cmd2.Connection = con

            con.Open()

            trm = con.BeginTransaction()

            cmd1.Transaction = trm

            cmd2.Transaction = trm

            cmd1.ExecuteNonQuery()

            cmd2.ExecuteNonQuery()

            trm.Commit()

        End Sub

    End Class

End Namespace


After running this code in table data we see that all records deleted where Location was England and a new record has been inserted in table. A very good example of transaction is banking money transaction.



Figure 2: After Transaction commit we can see the changed record.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON DOTNETHEAVEN (http://www.dotnetheaven.com/).

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor