In this tutorial, we will show you how to make a Transact-SQL transaction in a SQL Server database. We will use ASP.NET 2.0 and C# in the sample.

First, import the namespace of System.Data.SqlClient. The System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server. The.NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space.

We instantiate a Connections object to connect the sample database of Northwind. Then instantiate a SqlTransaction object, and associate it to Connections object. The next step is to instantiate a SqlCommand object, set the Transaction property of SqlCommand to SqlTransaction. After then, use SqlCommand to commit two Sql statements. As one of the statements is incorrect, the transaction will be rolled back on the error.

The front end Default.aspx page looks something like this:

The flow for the code behind page is as follows.

Download Source Files