using statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    #1

    using statement

    I have a using statement something like the following

    try
    {
    using(SqlConnec tion cnn = ...)
    {
    }
    catch(Exception e1)
    {
    }

    What happens to the sqlconnection, if the sql statment inside the
    using statement fails? Will it close the sql connection since it is
    inside the using?
    Thanks,
  • Jon Skeet [C# MVP]

    #2
    Re: using statement

    CSharper <csharper@gmx.c omwrote:
    try
    {
    using(SqlConnec tion cnn = ...)
    {
    }
    catch(Exception e1)
    {
    }
    >
    What happens to the sqlconnection, if the sql statment inside the
    using statement fails? Will it close the sql connection since it is
    inside the using?
    Yes.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Hans Kesting

      #3
      Re: using statement

      CSharper brought next idea :
      I have a using statement something like the following
      >
      try
      {
      using(SqlConnec tion cnn = ...)
      {
      }
      catch(Exception e1)
      {
      }
      >
      What happens to the sqlconnection, if the sql statment inside the
      using statement fails? Will it close the sql connection since it is
      inside the using?
      Thanks,
      Yes, the using statement is compiled similarly to

      SqlConnection cnn = null;
      try
      {
      cnn = ...;
      // the "using block"
      }
      finally
      {
      if (cnn != null)
      cnn.Dispose(); // which will close the connection
      }

      this does not "catch" any exceptions, so your own "catch" block can
      handle them (but the connection is closed before your "catch" block is
      activated).

      Hans Kesting


      Comment

      • CSharper

        #4
        Re: using statement

        On Sep 17, 10:34 am, Hans Kesting <news.han...@sp amgourmet.comwr ote:
        CSharper brought next idea :
        >
        I have a using statement something like the following
        >
        try
        {
           using(SqlConnec tion cnn = ...)
           {
           }
        catch(Exception e1)
        {
        }
        >
        What happens to the sqlconnection, if the sql statment inside the
        using statement fails? Will it close the sql connection since it is
        inside the using?
        Thanks,
        >
        Yes, the using statement is compiled similarly to
        >
         SqlConnection cnn = null;
         try
         {
           cnn = ...;
           // the "using block"
          }
          finally
          {
            if (cnn != null)
              cnn.Dispose(); // which will close the connection
          }
        >
        this does not "catch" any exceptions, so your own "catch" block can
        handle them (but the connection is closed before your "catch" block is
        activated).
        >
        Hans Kesting
        Thanks both.

        Comment

        Working...