.Net Interview Questions and Answers

Search:

What are transactions in C#? (.NET interview questions with answers)

By : Shiv Prasad Koirala | Sep 19th, 2013 | Views : 8298 | Comments : 0 Bookmark and Share

Transaction is a set of operations that should be considered as one unit.

 

Either every operations will executes successfully or none will. It should not end up in middle.

 

Best Example for transaction is money transactions in banks where X amount will be transferred from Account A to Account B. Steps are

 

1. Deduct Amount X from Account A

2. Add Amount X to Account B.

 

Let say after step 1 and before step 2 power failure happen. In that case step 1 will also get rolled back because both the steps together are considered as one Transaction

 

So how to do we implement a transaction in .NET?

 

To implement transactions we can use the transaction scope object as shown in the below code.

 

using(var scope = new TransactionScope())

 

    // Do all your insert,update and deletes here

 

    scope.Complete(); //if we make it here - commit the changes,

                      //if not - everything is rolled back

 

  }

 

How do we implement transactions in SQL Server  stored procedure?

 

To implement transaction in SQL Server we need use "Begin transaction" and "Commit" transaction as shown in the below code.

 

BEGIN TRANSACTION [Tran1]

 

BEGIN TRY

 

INSERT INTO [Test].[dbo].[T1]

  ([Title], [AVG])

VALUES ('xyz', 130), ('pqr', 230)

 

UPDATE [Test].[dbo].[T1]

  SET [Title] = N'abc' ,[AVG] = 1

WHERE [dbo].[T1].[Title] = N'az'

 

 

COMMIT TRANSACTION [Tran1]

 

END TRY

BEGIN CATCH

  ROLLBACK TRANSACTION [Tran1]

END CATCH 

 

How do we implement transaction using ADO.NET ?

 

Connection object of ADO.NET has a transaction object we can use that.

 

SqlConnection db = new SqlConnection("connstringhere");

      SqlTransaction transaction;

 

      db.Open();

      transaction = db.BeginTransaction();

      try

     

         new SqlCommand("INSERT INTO TransactionDemo " +

            "(Text) VALUES ('Row1');", db, transaction)

            .ExecuteNonQuery();

         new SqlCommand("INSERT INTO TransactionDemo " +

            "(Text) VALUES ('Row2');", db, transaction)

            .ExecuteNonQuery();

         new SqlCommand("INSERT INTO CrashMeNow VALUES " +

            "('Die', 'Die', 'Die');", db, transaction)

            .ExecuteNonQuery();

         transaction.Commit();

      }

      catch (SqlException sqlError)

     

         transaction.Rollback();

      }

      db.Close();

 

Do miss the below SQL Server interview question video :- Can views be updated ?

 

 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi