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
  1. Home
  2. General Programming
  3. C#
  4. SqlTransaction object

SqlTransaction object

Scheduled Pinned Locked Moved C#
question
14 Posts 3 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.
  • V Vipin d

    Thank you very much for your reply..:) using (SqlConnection connection = DbConnection("SerConstr")) { using (SqlCommand command = connection.CreateCommand()) { SqlTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); command.Transaction = transaction; // i need to perform this in con1 command.CommandText = "Insert ..."; command.ExecuteNonQuery(); // Execute i need to perform this in con2 command.CommandText = "Update..."; command.ExecuteNonQuery(); transaction.Commit(); } catch { transaction.Rollback(); throw; } finally { connection.Close(); } how I ll do this??

    They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
    CrazySanker

    A Offline
    A Offline
    Andrei Ungureanu
    wrote on last edited by
    #5

    Vipin.d wrote:

    // i need to perform this in con1 command.CommandText = "Insert ..."; command.ExecuteNonQuery(); // Execute i need to perform this in con2 command.CommandText = "Update..."; command.ExecuteNonQuery();

    The insert and update queries refer to the same database, within the same server?

    I will use Google before asking dumb questions

    V 1 Reply Last reply
    0
    • A Andrei Ungureanu

      Why do you need 2 connection strings? What is the purpose..changing the database while the transaction is open?

      I will use Google before asking dumb questions

      V Offline
      V Offline
      Vipin d
      wrote on last edited by
      #6

      Dear friend, my rquiremnt is like 1. SELECT some data from servr1.db1.Tbl1 2. INSERT that to server2.db2.Tbl1. 3. UPDATE the status filed of selected data in servr1.db1.Tbl1 If either step 2 or 3 fail I need to Roll back that transaction. any advice please..

      They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
      CrazySanker

      A 1 Reply Last reply
      0
      • V Vipin d

        Thank you very much for your reply..:) using (SqlConnection connection = DbConnection("SerConstr")) { using (SqlCommand command = connection.CreateCommand()) { SqlTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); command.Transaction = transaction; // i need to perform this in con1 command.CommandText = "Insert ..."; command.ExecuteNonQuery(); // Execute i need to perform this in con2 command.CommandText = "Update..."; command.ExecuteNonQuery(); transaction.Commit(); } catch { transaction.Rollback(); throw; } finally { connection.Close(); } how I ll do this??

        They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
        CrazySanker

        A Offline
        A Offline
        Adeel Chaudhry
        wrote on last edited by
        #7

        Can you please elaborate "Insert..." & "Update..." bit more? :) Like for what purpose you need to have two CSs? Do you want to change the database? If yes, why not do it as an intermediate SQL statement between insert & update?

        V 1 Reply Last reply
        0
        • A Andrei Ungureanu

          Vipin.d wrote:

          // i need to perform this in con1 command.CommandText = "Insert ..."; command.ExecuteNonQuery(); // Execute i need to perform this in con2 command.CommandText = "Update..."; command.ExecuteNonQuery();

          The insert and update queries refer to the same database, within the same server?

          I will use Google before asking dumb questions

          V Offline
          V Offline
          Vipin d
          wrote on last edited by
          #8

          yes i know that , my requirement is I need to change the connection string before doing UPDATE process.is it have any solution??

          They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
          CrazySanker

          1 Reply Last reply
          0
          • A Adeel Chaudhry

            Can you please elaborate "Insert..." & "Update..." bit more? :) Like for what purpose you need to have two CSs? Do you want to change the database? If yes, why not do it as an intermediate SQL statement between insert & update?

            V Offline
            V Offline
            Vipin d
            wrote on last edited by
            #9

            Dear friend, my rquiremnt is like 1. SELECT some data from servr1.db1.Tbl1 2. INSERT that to server2.db2.Tbl1. 3. UPDATE the status filed of selected data in servr1.db1.Tbl1 If either step 2 or 3 fail I need to Roll back that transaction. any advice please.. Let me check intermediate SQL statement in my Google Uncle:)

            They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
            CrazySanker

            A 1 Reply Last reply
            0
            • V Vipin d

              Dear friend, my rquiremnt is like 1. SELECT some data from servr1.db1.Tbl1 2. INSERT that to server2.db2.Tbl1. 3. UPDATE the status filed of selected data in servr1.db1.Tbl1 If either step 2 or 3 fail I need to Roll back that transaction. any advice please.. Let me check intermediate SQL statement in my Google Uncle:)

              They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
              CrazySanker

              A Offline
              A Offline
              Adeel Chaudhry
              wrote on last edited by
              #10

              First, allow me to clear this, I used "intermediate" as an english word, not as technical word. :) Theoretically, it should be like that connect with server1 select if it was a success connect with server2 insert if it was a success disconnect with server2 update if not success rollback disconnect with server1 Surely, you will need to have 2 connection objects, given this is not a live server with few hundereds transactions goin on. Hope that helps! Adeel

              V 1 Reply Last reply
              0
              • A Adeel Chaudhry

                First, allow me to clear this, I used "intermediate" as an english word, not as technical word. :) Theoretically, it should be like that connect with server1 select if it was a success connect with server2 insert if it was a success disconnect with server2 update if not success rollback disconnect with server1 Surely, you will need to have 2 connection objects, given this is not a live server with few hundereds transactions goin on. Hope that helps! Adeel

                V Offline
                V Offline
                Vipin d
                wrote on last edited by
                #11

                :doh: ohh!!I already started searching the term intermediate sql :cool: thank you for your reply..:-D

                They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
                CrazySanker

                1 Reply Last reply
                0
                • V Vipin d

                  Dear friend, my rquiremnt is like 1. SELECT some data from servr1.db1.Tbl1 2. INSERT that to server2.db2.Tbl1. 3. UPDATE the status filed of selected data in servr1.db1.Tbl1 If either step 2 or 3 fail I need to Roll back that transaction. any advice please..

                  They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
                  CrazySanker

                  A Offline
                  A Offline
                  Andrei Ungureanu
                  wrote on last edited by
                  #12

                  The ConnectionString property can be set only when the connection is closed. (from MSDN) and when you close a query, the pending transactions are rolledback. So...my suggestion is to use at least 2 connections

                  using (SqlConnection conn = new SqlConnection())
                  {
                  //set the first cs
                  string cs1 = "connection string 1";
                  conn.ConnectionString = cs1;

                  conn.Open();

                  SqlTransaction trans = conn.BeginTransaction();

                  //do the the select query

                  if (InsertSecondDB())
                  {
                  //update first DB
                  trans.Commit();
                  }
                  else
                  {
                  trans.Rollback();
                  }

                  conn.Close();
                  }

                  The InsertSecondDB should create a new connection and a new transaction to the second DataBase and return true if the query succeeded or false otherwise. Hope it helps.

                  I will use Google before asking dumb questions

                  V 1 Reply Last reply
                  0
                  • A Andrei Ungureanu

                    The ConnectionString property can be set only when the connection is closed. (from MSDN) and when you close a query, the pending transactions are rolledback. So...my suggestion is to use at least 2 connections

                    using (SqlConnection conn = new SqlConnection())
                    {
                    //set the first cs
                    string cs1 = "connection string 1";
                    conn.ConnectionString = cs1;

                    conn.Open();

                    SqlTransaction trans = conn.BeginTransaction();

                    //do the the select query

                    if (InsertSecondDB())
                    {
                    //update first DB
                    trans.Commit();
                    }
                    else
                    {
                    trans.Rollback();
                    }

                    conn.Close();
                    }

                    The InsertSecondDB should create a new connection and a new transaction to the second DataBase and return true if the query succeeded or false otherwise. Hope it helps.

                    I will use Google before asking dumb questions

                    V Offline
                    V Offline
                    Vipin d
                    wrote on last edited by
                    #13

                    defenitely it clicked some ideas in my mind.. Thank you soo much!!

                    They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
                    CrazySanker

                    1 Reply Last reply
                    0
                    • V Vipin d

                      hi, Is it posible to use two diferent connection string in a SqlTransaction object?? Be very grateful for any advice.

                      They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
                      CrazySanker

                      V Offline
                      V Offline
                      Vipin d
                      wrote on last edited by
                      #14

                      Dear friends, During my g0ole search i got this Distributed Transaction ^]

                      They laugh at me; they think I’m different. I laugh at them, ‘coz they are all same!!
                      CrazySanker

                      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