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. Anyway to check if a SqlConnection has a SqlTransaction open against it

Anyway to check if a SqlConnection has a SqlTransaction open against it

Scheduled Pinned Locked Moved C#
question
5 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.
  • D Offline
    D Offline
    dlarkin77
    wrote on last edited by
    #1

    Hi, Is there any way to find out if a SqlConnection has an open SqlTransaction? Basically I have an object with a method that accepts a SqlConnection as a parameter. This method is called from a number of different application. Sometimes the SqlConnection has a SqlTransaction and other times it doesn't. So what I need to be able to do is something along the lines of

    public bool Post(SqlConnection cn, int ID)
    {
    SqlTransaction trans;

    if(cn.HasTransaction)
    {
    trans = cn.Transaction;
    }
    else
    {
    trans = cn.BeginTransaction();
    }

    // some other stuff

    return true;
    }

    Is there anyway to get something similar? Thanks, David

    P P 2 Replies Last reply
    0
    • D dlarkin77

      Hi, Is there any way to find out if a SqlConnection has an open SqlTransaction? Basically I have an object with a method that accepts a SqlConnection as a parameter. This method is called from a number of different application. Sometimes the SqlConnection has a SqlTransaction and other times it doesn't. So what I need to be able to do is something along the lines of

      public bool Post(SqlConnection cn, int ID)
      {
      SqlTransaction trans;

      if(cn.HasTransaction)
      {
      trans = cn.Transaction;
      }
      else
      {
      trans = cn.BeginTransaction();
      }

      // some other stuff

      return true;
      }

      Is there anyway to get something similar? Thanks, David

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I rather suspect that you could just check to see if the Transaction is null or not on the command.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      D 1 Reply Last reply
      0
      • D dlarkin77

        Hi, Is there any way to find out if a SqlConnection has an open SqlTransaction? Basically I have an object with a method that accepts a SqlConnection as a parameter. This method is called from a number of different application. Sometimes the SqlConnection has a SqlTransaction and other times it doesn't. So what I need to be able to do is something along the lines of

        public bool Post(SqlConnection cn, int ID)
        {
        SqlTransaction trans;

        if(cn.HasTransaction)
        {
        trans = cn.Transaction;
        }
        else
        {
        trans = cn.BeginTransaction();
        }

        // some other stuff

        return true;
        }

        Is there anyway to get something similar? Thanks, David

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Shouldn't you track that yourself?

        D 1 Reply Last reply
        0
        • P Pete OHanlon

          I rather suspect that you could just check to see if the Transaction is null or not on the command.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          D Offline
          D Offline
          dlarkin77
          wrote on last edited by
          #4

          There is no command being passed in, just a connection. So when I create a command object I want to do something like.

          SqlTransaction trans = null;
          cmd.Connection = cn;

          if(cn.HasTransaction)
          {
          trans = cn.Transaction;
          }
          else
          {
          trans = cn.BeginTransaction();
          }

          cmd.Transaction = trans;

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Shouldn't you track that yourself?

            D Offline
            D Offline
            dlarkin77
            wrote on last edited by
            #5

            Ideally we would/should be. But there are so many applications using this class at the moment that it's not really feasible to revisit them all right now.

            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