Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Begin-Commit Transaction in .NET

Begin-Commit Transaction in .NET

Scheduled Pinned Locked Moved C#
helpcsharp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Coding C
    wrote on last edited by
    #1

    Hi, i am facing a problem plz if someone can help me. in visual basic i normally used to do con.begintrans any no. of execute commands con.committrans on error i can do con.rollbacktrans is there something in .net which relates to this technique. thanks.:) Nitin...

    C G D 3 Replies Last reply
    0
    • C Coding C

      Hi, i am facing a problem plz if someone can help me. in visual basic i normally used to do con.begintrans any no. of execute commands con.committrans on error i can do con.rollbacktrans is there something in .net which relates to this technique. thanks.:) Nitin...

      C Offline
      C Offline
      coolestCoder
      wrote on last edited by
      #2

      Hi Nitin, Please see the following link for the solution http://windowssdk.msdn.microsoft.com/en-us/library/2k2hy99x.aspx[^]


      "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


      Anant Y. Kulkarni

      G 1 Reply Last reply
      0
      • C Coding C

        Hi, i am facing a problem plz if someone can help me. in visual basic i normally used to do con.begintrans any no. of execute commands con.committrans on error i can do con.rollbacktrans is there something in .net which relates to this technique. thanks.:) Nitin...

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Here is an example from MSDN:

        using (SqlConnection connection = new SqlConnection(connectString))
        {
        connection.Open();

        // Start a local transaction.
        SqlTransaction sqlTran = connection.BeginTransaction();
        
        // Enlist the command in the current transaction.
        SqlCommand command = connection.CreateCommand();
        command.Transaction = sqlTran;
        
        try
        {
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
            command.ExecuteNonQuery();
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
            command.ExecuteNonQuery();
            sqlTran.Commit();
            Console.WriteLine("Both records were written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Neither record was written to database.");
            sqlTran.Rollback();
        }
        

        }

        --- b { font-weight: normal; }

        C 1 Reply Last reply
        0
        • C coolestCoder

          Hi Nitin, Please see the following link for the solution http://windowssdk.msdn.microsoft.com/en-us/library/2k2hy99x.aspx[^]


          "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


          Anant Y. Kulkarni

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Spooky. It's the second time today that I post the exact same answer at the same time as someone else. :~

          --- b { font-weight: normal; }

          1 Reply Last reply
          0
          • G Guffa

            Here is an example from MSDN:

            using (SqlConnection connection = new SqlConnection(connectString))
            {
            connection.Open();

            // Start a local transaction.
            SqlTransaction sqlTran = connection.BeginTransaction();
            
            // Enlist the command in the current transaction.
            SqlCommand command = connection.CreateCommand();
            command.Transaction = sqlTran;
            
            try
            {
                command.CommandText =
                  "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
                command.ExecuteNonQuery();
                command.CommandText =
                  "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
                command.ExecuteNonQuery();
                sqlTran.Commit();
                Console.WriteLine("Both records were written to database.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Neither record was written to database.");
                sqlTran.Rollback();
            }
            

            }

            --- b { font-weight: normal; }

            C Offline
            C Offline
            Coding C
            wrote on last edited by
            #5

            Hi Guffa! thanks for reply. its very usefull. thank You.:) Nitin...

            1 Reply Last reply
            0
            • C Coding C

              Hi, i am facing a problem plz if someone can help me. in visual basic i normally used to do con.begintrans any no. of execute commands con.committrans on error i can do con.rollbacktrans is there something in .net which relates to this technique. thanks.:) Nitin...

              D Offline
              D Offline
              Dustin Metzgar
              wrote on last edited by
              #6

              The link and code provided in earlier responses do work well, but they are old ways of performing transactions. .Net 2.0 has a thing called TransactionScope in the System.Transactions namespace.

              using (TransactionScope scope = new TransactionScope())
              {
              using (SqlConnection connection = new SqlConnection(connectString))
              {
              connection.Open();

                  SqlCommand command = connection.CreateCommand();
                  try
                  {
                      command.CommandText =
                        "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
                      command.ExecuteNonQuery();
                      command.CommandText =
                        "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
                      command.ExecuteNonQuery();
                      scope.Complete();
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                      Console.WriteLine("Neither record was written to database.");
                  }
              }
              

              }

              TransactionScope has the ability to nest transactions and will also automatically upgrade a transaction into a distributed transaction if you start talking to two or more different databases. And when .Net 3 and WCF come along, WCF services will also be able to participate in the transaction.


              Logifusion[^] If not entertaining, write your Congressman.

              C 1 Reply Last reply
              0
              • D Dustin Metzgar

                The link and code provided in earlier responses do work well, but they are old ways of performing transactions. .Net 2.0 has a thing called TransactionScope in the System.Transactions namespace.

                using (TransactionScope scope = new TransactionScope())
                {
                using (SqlConnection connection = new SqlConnection(connectString))
                {
                connection.Open();

                    SqlCommand command = connection.CreateCommand();
                    try
                    {
                        command.CommandText =
                          "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
                        command.ExecuteNonQuery();
                        command.CommandText =
                          "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
                        command.ExecuteNonQuery();
                        scope.Complete();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine("Neither record was written to database.");
                    }
                }
                

                }

                TransactionScope has the ability to nest transactions and will also automatically upgrade a transaction into a distributed transaction if you start talking to two or more different databases. And when .Net 3 and WCF come along, WCF services will also be able to participate in the transaction.


                Logifusion[^] If not entertaining, write your Congressman.

                C Offline
                C Offline
                Coding C
                wrote on last edited by
                #7

                Hi Dustin Metzgar , thanks for better way. thanq you very much.:) Regards. Nitin...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups