1

How to implement two table insert code with SQL transaction at front end?

I have two tables, TblMaster and TblSub. If Tblsub insert fails, then TblMaster record should be roll back.

I have a common function ExecuteNonQuery in SQL helper class for inserting records.

Please suggest me way to resolve this issue.

user2126132
  • 13
  • 1
  • 4

3 Answers3

3

An example in C#

SqlTransaction transaction = null;
SqlConnection con = null;

// they will be used to decide whether to commit or rollback the transaction
bool debitResult = false;
bool creditResult = false;

try
{
    con = new SqlConnection(CONNECTION_STRING);
    con.Open();

    // lets begin a transaction here
    transaction = con.BeginTransaction();

    // Let us do a debit first
    using (SqlCommand cmd = con.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Insert into Table1";   // Query here

        // assosiate this command with transaction
        cmd.Transaction = transaction;

        debitResult = cmd.ExecuteNonQuery() == 1;
    }

    // A dummy throw just to check whether the transaction are working or not
    //throw new Exception("Let see..."); // uncomment this line to see the transaction in action

    // And now do a credit
    using (SqlCommand cmd = con.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Insert into Table2";   // Query here

        // assosiate this command with transaction
        cmd.Transaction = transaction;

        creditResult = cmd.ExecuteNonQuery() == 1;
    }

    if (debitResult && creditResult)
    {
        transaction.Commit();
    }
}
catch
{
    transaction.Rollback();            
}
finally
{
    con.Close();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pravprab
  • 2,301
  • 3
  • 26
  • 43
1

Try this way

BEGIN TRANSACTION tran1
  BEGIN  TRY
    --Insert into Table1
    --Insert into Table2
COMMIT TRANSACTION  tran1                           
  END TRY

BEGIN CATCH
   ROLLBACK TRANSACTION tran1 
      raiserror('Cannot commite transaction',16,1,@@error);
   return;
END CATCH
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • I want to use it in a common function executenonquery(). And this method is used every time while inserting record. – user2126132 Jan 17 '14 at 05:30
  • as you said SqlTransaction so this way you perform SqlTransaction but now you saying you want to do it from a function from fromt end so please update your question clearly – Amit Bisht Jan 17 '14 at 05:35
0

Use a transaction scope like this (you need to reference the System.Transactions .NET library in your project by using the "Add reference" menu item):

using (TransactionScope scope = new TransactionScope())
{
    using (SqlConnection conn = new SqlConnection(<connectionString>))
    {
        conn.Open()

        try
        {
            // Do your stuff
            ...

            // Commit the transaction
            scope.Complete();
        }
        catch (...)
        {
            // Handle exceptions, transaction is rolled back automatically, as
            // "Complete" was not called
        }
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139