Using keyword

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

    #1

    Using keyword

    Can anyone here confirm for sure that reader.Close() and Dispose() will
    get called using the following routine:

    using(SqlDataRe ader reader = GetOpenReaderFu nction())
    {
    while(reader.Re ad()) doSomething(rea der);
    }

    I have seen examples here and there with mixed assumptions.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Using keyword

    INeedADip,

    Yes, it will definitely close. For all intents and purposes, the C#
    compiler translates that into:

    SqlDataReader reader = GetOpenReaderFu nction();

    try
    {
    while(reader.Re ad()) doSomething(rea der);
    }
    finally
    {
    if (reader != null)
    {
    ((IDispose) reader).Dispose ();
    }
    }

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "INeedADip" <ineedadip@gmai l.com> wrote in message
    news:1136493228 .263735.263240@ g47g2000cwa.goo glegroups.com.. .[color=blue]
    > Can anyone here confirm for sure that reader.Close() and Dispose() will
    > get called using the following routine:
    >
    > using(SqlDataRe ader reader = GetOpenReaderFu nction())
    > {
    > while(reader.Re ad()) doSomething(rea der);
    > }
    >
    > I have seen examples here and there with mixed assumptions.
    >[/color]


    Comment

    • John Wood

      #3
      Re: Using keyword

      Dispose will be called automatically when the scope under the using
      statement exits.

      And Dispose will call Close().

      "INeedADip" <ineedadip@gmai l.com> wrote in message
      news:1136493228 .263735.263240@ g47g2000cwa.goo glegroups.com.. .[color=blue]
      > Can anyone here confirm for sure that reader.Close() and Dispose() will
      > get called using the following routine:
      >
      > using(SqlDataRe ader reader = GetOpenReaderFu nction())
      > {
      > while(reader.Re ad()) doSomething(rea der);
      > }
      >
      > I have seen examples here and there with mixed assumptions.
      >[/color]


      Comment

      • Tom Porterfield

        #4
        Re: Using keyword

        INeedADip wrote:[color=blue]
        > Can anyone here confirm for sure that reader.Close() and Dispose() will
        > get called using the following routine:
        >
        > using(SqlDataRe ader reader = GetOpenReaderFu nction())
        > {
        > while(reader.Re ad()) doSomething(rea der);
        > }
        >
        > I have seen examples here and there with mixed assumptions.
        >[/color]

        Dispose will be called when the using block is exited, whether through
        normal code execution or if an exception occurs. The Dispose method of
        SqlDataReader calls Close to insure that the connection has been closed.

        As a generic statement, using insures that Dispose will be called. It
        is up to the implementation of Dispose that insures resources are
        properly released.
        --
        Tom Porterfield

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: Using keyword

          Hi,

          "INeedADip" <ineedadip@gmai l.com> wrote in message
          news:1136493228 .263735.263240@ g47g2000cwa.goo glegroups.com.. .[color=blue]
          > Can anyone here confirm for sure that reader.Close() and Dispose() will
          > get called using the following routine:
          >
          > using(SqlDataRe ader reader = GetOpenReaderFu nction())
          > {
          > while(reader.Re ad()) doSomething(rea der);
          > }
          >
          > I have seen examples here and there with mixed assumptions.[/color]

          To be exact it's Dispose which is called and in this particular case Dispose
          does call Close.

          Note that you still need to close the connection, as the closing of the
          DataReader does not close it.
          UNLESS you created your reader using SqlCommand.Exec uteReader(
          CommandBehavior .CloseConnectio n );



          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation


          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: Using keyword

            Hi Tom,


            [color=blue]
            > Dispose will be called when the using block is exited, whether through
            > normal code execution or if an exception occurs. The Dispose method of
            > SqlDataReader calls Close to insure that the connection has been closed.[/color]

            It's the Close of the datareader or the one of the connection the one that
            is called?
            I was under the impression it was the Reader, as this is needed to have
            access to stuff like, output parameters, records count. and return values.

            The connection is only closed when the correct CommandBehaviou r is passed in
            the ExecuteReader

            Correct me if I'm wrong



            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Using keyword

              INeedADip <ineedadip@gmai l.com> wrote:[color=blue]
              > Can anyone here confirm for sure that reader.Close() and Dispose() will
              > get called using the following routine:
              >
              > using(SqlDataRe ader reader = GetOpenReaderFu nction())
              > {
              > while(reader.Re ad()) doSomething(rea der);
              > }
              >
              > I have seen examples here and there with mixed assumptions.[/color]

              Close() won't be called, but Dispose() will be (which is enough).

              The only problem would be if GetOpenReaderFu nction() acquired a
              SqlDataReader but then threw an exception before returning it.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • John Wood

                #8
                Re: Using keyword

                > Close() won't be called, but Dispose() will be (which is enough). <
                Well, Close will be called by Dispose. (at least according to the
                disassembled code for SqlDataReader).
                But you probably mean that the using clause calls dispose directly, not
                close.

                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.1e2799 6480a020b198cc7 7@msnews.micros oft.com...[color=blue]
                > INeedADip <ineedadip@gmai l.com> wrote:[color=green]
                > > Can anyone here confirm for sure that reader.Close() and Dispose() will
                > > get called using the following routine:
                > >
                > > using(SqlDataRe ader reader = GetOpenReaderFu nction())
                > > {
                > > while(reader.Re ad()) doSomething(rea der);
                > > }
                > >
                > > I have seen examples here and there with mixed assumptions.[/color]
                >
                > Close() won't be called, but Dispose() will be (which is enough).
                >
                > The only problem would be if GetOpenReaderFu nction() acquired a
                > SqlDataReader but then threw an exception before returning it.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Using keyword

                  John Wood <john.wood@prio rganize_nospam_ _.com> wrote:[color=blue][color=green]
                  > > Close() won't be called, but Dispose() will be (which is enough). <[/color]
                  > Well, Close will be called by Dispose. (at least according to the
                  > disassembled code for SqlDataReader).
                  > But you probably mean that the using clause calls dispose directly, not
                  > close.[/color]

                  Exactly :)

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  • Tom Porterfield

                    #10
                    Re: Using keyword

                    Ignacio Machin ( .NET/ C# MVP ) wrote:[color=blue]
                    > Correct me if I'm wrong[/color]

                    You are correct, it is the reader that is closed, not the connection.
                    Thanks.
                    --
                    Tom Porterfield

                    Comment

                    • Ignacio Machin \( .NET/ C# MVP \)

                      #11
                      Re: Using keyword

                      Hi,


                      "John Wood" <john.wood@prio rganize_nospam_ _.com> wrote in message
                      news:%23lcpc4jE GHA.208@tk2msft ngp13.phx.gbl.. .[color=blue][color=green]
                      >> Close() won't be called, but Dispose() will be (which is enough). <[/color]
                      > Well, Close will be called by Dispose. (at least according to the
                      > disassembled code for SqlDataReader).
                      > But you probably mean that the using clause calls dispose directly, not
                      > close.[/color]

                      A bad thing is this is not clearly stated in the documentation. I did not
                      check msdn2 though.



                      --
                      Ignacio Machin,
                      ignacio.machin AT dot.state.fl.us
                      Florida Department Of Transportation


                      Comment

                      Working...