Should I Dispose() an SqlCommand object?

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

    #1

    Should I Dispose() an SqlCommand object?

    Hello!

    After I've finished using an instance of the SqlCommand class, should I then
    invoke Dispose() on the instance. I suppose so, as there is a Dispose
    method, but what does it actually release?

    I would basically prefer to skip invoking Dispose() as this will free me
    from determining when the usage actually has finished.


    Best regards,

    Henrik Dahl


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Should I Dispose() an SqlCommand object?

    Henrik,

    This is a bad design, in my opinion. Generally speaking, with DB operations, at least with the model in .NET (disconnected recordsets), you should open your connection, perform your operation, and get out. You can recreate the command. Also, do you really want to be passing around commands to your database to someone on the outside that might use it in an improper manner?

    So, given all that, yes, you should always call Dispose. It doesn't really matter what it disposes, the implication through the implementation (say that 10 times fast) of IDispose is that there is a resource the class manages which should be disposed of in a timely manner as opposed to waiting for a GC.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(d0t)paldin o=At-exisconsulting' dot|com

    On Mon, 6 Oct 2003 12:57:18 +0200, Henrik Dahl <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote:
    [color=blue]
    > Hello!
    >
    > After I've finished using an instance of the SqlCommand class, should I then
    > invoke Dispose() on the instance. I suppose so, as there is a Dispose
    > method, but what does it actually release?
    >
    > I would basically prefer to skip invoking Dispose() as this will free me
    > from determining when the usage actually has finished.
    >
    >
    > Best regards,
    >
    > Henrik Dahl
    >
    >[/color]


    Comment

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

      #3
      Re: Should I Dispose() an SqlCommand object?

      Hi Henrik,

      I'm not very sure that you need to call Dispose() on a SqlCommand , the
      Dispose() of SqlCommand is inherited from Component ( according to MSDN )
      therefore it does not perform any DB related op.
      You should definely close the connection , regarding the SqlCommand this
      is what the ADO.NET guide says:

      "Although you can repeatedly use the same SqlCommand object to execute the
      same command multiple times, do not reuse the same SqlCommand object to
      execute different commands. "


      Hope this help,

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

      "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
      news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...[color=blue]
      > Hello!
      >
      > After I've finished using an instance of the SqlCommand class, should I[/color]
      then[color=blue]
      > invoke Dispose() on the instance. I suppose so, as there is a Dispose
      > method, but what does it actually release?
      >
      > I would basically prefer to skip invoking Dispose() as this will free me
      > from determining when the usage actually has finished.
      >
      >
      > Best regards,
      >
      > Henrik Dahl
      >
      >[/color]


      Comment

      • Henrik Dahl

        #4
        Re: Should I Dispose() an SqlCommand object?

        Hello Ignacio,

        Interesting, also because on the SqlCommand overview page it reads:
        "You can reset the CommandText property and reuse the SqlCommand object.
        However, you must close the SqlDataReader before you can execute a new or
        previous command.".


        Best regards,

        Henrik Dahl

        "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
        in message news:uwEfdlAjDH A.2564@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hi Henrik,
        >
        > I'm not very sure that you need to call Dispose() on a SqlCommand , the
        > Dispose() of SqlCommand is inherited from Component ( according to MSDN )
        > therefore it does not perform any DB related op.
        > You should definely close the connection , regarding the SqlCommand this
        > is what the ADO.NET guide says:
        >
        > "Although you can repeatedly use the same SqlCommand object to execute the
        > same command multiple times, do not reuse the same SqlCommand object to
        > execute different commands. "
        >
        >
        > Hope this help,
        >
        > --
        > Ignacio Machin,
        > ignacio.machin AT dot.state.fl.us
        > Florida Department Of Transportation
        >
        > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
        > news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...[color=green]
        > > Hello!
        > >
        > > After I've finished using an instance of the SqlCommand class, should I[/color]
        > then[color=green]
        > > invoke Dispose() on the instance. I suppose so, as there is a Dispose
        > > method, but what does it actually release?
        > >
        > > I would basically prefer to skip invoking Dispose() as this will free me
        > > from determining when the usage actually has finished.
        > >
        > >
        > > Best regards,
        > >
        > > Henrik Dahl
        > >
        > >[/color]
        >
        >[/color]


        Comment

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

          #5
          Re: Should I Dispose() an SqlCommand object?

          Hi,

          Of course you can do it, but it seems that it's not advised, it's like
          maintaining open a connection during the live of a application, is doable ,
          but not very wise.

          The DataReader keeps the connection closed in a way that nobody can reuse
          it until it gets close, that's way SqlCommand.Exec uteReader has a overload
          that receive a CommandBehavior that you can set it to CloseConnection and it
          will automatically close the connection once you close the reader.


          Cheers,

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

          "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
          news:eOua$zAjDH A.1284@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Hello Ignacio,
          >
          > Interesting, also because on the SqlCommand overview page it reads:
          > "You can reset the CommandText property and reuse the SqlCommand object.
          > However, you must close the SqlDataReader before you can execute a new or
          > previous command.".
          >
          >
          > Best regards,
          >
          > Henrik Dahl
          >
          > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
          wrote[color=blue]
          > in message news:uwEfdlAjDH A.2564@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Hi Henrik,
          > >
          > > I'm not very sure that you need to call Dispose() on a SqlCommand , the
          > > Dispose() of SqlCommand is inherited from Component ( according to[/color][/color]
          MSDN )[color=blue][color=green]
          > > therefore it does not perform any DB related op.
          > > You should definely close the connection , regarding the SqlCommand[/color][/color]
          this[color=blue][color=green]
          > > is what the ADO.NET guide says:
          > >
          > > "Although you can repeatedly use the same SqlCommand object to execute[/color][/color]
          the[color=blue][color=green]
          > > same command multiple times, do not reuse the same SqlCommand object to
          > > execute different commands. "
          > >
          > >
          > > Hope this help,
          > >
          > > --
          > > Ignacio Machin,
          > > ignacio.machin AT dot.state.fl.us
          > > Florida Department Of Transportation
          > >
          > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
          > > news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...[color=darkred]
          > > > Hello!
          > > >
          > > > After I've finished using an instance of the SqlCommand class, should[/color][/color][/color]
          I[color=blue][color=green]
          > > then[color=darkred]
          > > > invoke Dispose() on the instance. I suppose so, as there is a Dispose
          > > > method, but what does it actually release?
          > > >
          > > > I would basically prefer to skip invoking Dispose() as this will free[/color][/color][/color]
          me[color=blue][color=green][color=darkred]
          > > > from determining when the usage actually has finished.
          > > >
          > > >
          > > > Best regards,
          > > >
          > > > Henrik Dahl
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Henrik Dahl

            #6
            Re: Should I Dispose() an SqlCommand object?

            Nicholas,

            Obviously I generally agree with you completely. In this particular case I
            find it a bit surprising that I've seen many examples of using SqlCommand
            objects in documentation from Microsoft, but I've never seen one which
            actually Disposes the SqlCommand object afterwards, have you?


            Best regards,

            Henrik Dahl

            "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
            in message news:oprwmf6mvn a4u15p@msnews.m icrosoft.com...[color=blue]
            > Henrik,
            >
            > This is a bad design, in my opinion. Generally speaking, with DB[/color]
            operations, at least with the model in .NET (disconnected recordsets), you
            should open your connection, perform your operation, and get out. You can
            recreate the command. Also, do you really want to be passing around
            commands to your database to someone on the outside that might use it in an
            improper manner?[color=blue]
            >
            > So, given all that, yes, you should always call Dispose. It doesn't[/color]
            really matter what it disposes, the implication through the implementation
            (say that 10 times fast) of IDispose is that there is a resource the class
            manages which should be disposed of in a timely manner as opposed to waiting
            for a GC.[color=blue]
            >
            > Hope this helps.
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - nick(d0t)paldin o=At-exisconsulting' dot|com
            >
            > On Mon, 6 Oct 2003 12:57:18 +0200, Henrik Dahl[/color]
            <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote:[color=blue]
            >[color=green]
            > > Hello!
            > >
            > > After I've finished using an instance of the SqlCommand class, should I[/color][/color]
            then[color=blue][color=green]
            > > invoke Dispose() on the instance. I suppose so, as there is a Dispose
            > > method, but what does it actually release?
            > >
            > > I would basically prefer to skip invoking Dispose() as this will free me
            > > from determining when the usage actually has finished.
            > >
            > >
            > > Best regards,
            > >
            > > Henrik Dahl
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Angel Saenz-Badillos[MS]

              #7
              Re: Should I Dispose() an SqlCommand object?

              You should call Dispose on any object that implements IDisposable, this is
              very easy to do with C# and the "using" keyword, in other .net languages you
              can use the try finally construct to ensure that Dispose gets called. In
              general adhering to this basic principle will greatly reduce stress related
              problems with your code and may increase performance as it makes cleaning up
              smarter.

              Something else to think about is that allthough currently the Command may
              not be doing anything meaningfull in its dispose method, there is no
              guarantee that this will be true for future releases of the framework.
              Calling dispose for all disposable objects will greatly enhance the lifetime
              of your code.

              Hope this helped,
              --
              Angel Saenz-Badillos [MS] Managed Providers
              This posting is provided "AS IS", with no warranties, and confers no rights.
              Please do not send email directly to this alias. This alias is for newsgroup
              purposes only.
              "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
              news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...[color=blue]
              > Hello!
              >
              > After I've finished using an instance of the SqlCommand class, should I[/color]
              then[color=blue]
              > invoke Dispose() on the instance. I suppose so, as there is a Dispose
              > method, but what does it actually release?
              >
              > I would basically prefer to skip invoking Dispose() as this will free me
              > from determining when the usage actually has finished.
              >
              >
              > Best regards,
              >
              > Henrik Dahl
              >
              >[/color]


              Comment

              • Henrik Dahl

                #8
                Re: Should I Dispose() an SqlCommand object?

                Angel,

                Yes, that's obvious, but how may it then be that there are many, many
                examples from Microsoft which do not invoke the Dispose() method of
                SqlCommand objects, even examples which include Disposing e.g. the
                SqlConnection object?

                A question:
                In one of the books from Microsoft Press it's stressed that you should
                Close(), not Dispose(), an SqlConnection object because Dispose() will close
                the connection to MS SQL Server whereas Close() lets the connection to go
                back to the pool, i.e. closes the logical connection. In the ".NET Data
                Access Architecture Guide" it's claimed that both Close() and Dispose() let
                the SqlConnection to go back to the pool. Which is correct?


                Best regards,

                Henrik Dahl

                "Angel Saenz-Badillos[MS]" <angelsa@online .microsoft.com> wrote in message
                news:uvLde$PjDH A.1964@TK2MSFTN GP12.phx.gbl...[color=blue]
                > You should call Dispose on any object that implements IDisposable, this is
                > very easy to do with C# and the "using" keyword, in other .net languages[/color]
                you[color=blue]
                > can use the try finally construct to ensure that Dispose gets called. In
                > general adhering to this basic principle will greatly reduce stress[/color]
                related[color=blue]
                > problems with your code and may increase performance as it makes cleaning[/color]
                up[color=blue]
                > smarter.
                >
                > Something else to think about is that allthough currently the Command may
                > not be doing anything meaningfull in its dispose method, there is no
                > guarantee that this will be true for future releases of the framework.
                > Calling dispose for all disposable objects will greatly enhance the[/color]
                lifetime[color=blue]
                > of your code.
                >
                > Hope this helped,
                > --
                > Angel Saenz-Badillos [MS] Managed Providers
                > This posting is provided "AS IS", with no warranties, and confers no[/color]
                rights.[color=blue]
                > Please do not send email directly to this alias. This alias is for[/color]
                newsgroup[color=blue]
                > purposes only.
                > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                > news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...[color=green]
                > > Hello!
                > >
                > > After I've finished using an instance of the SqlCommand class, should I[/color]
                > then[color=green]
                > > invoke Dispose() on the instance. I suppose so, as there is a Dispose
                > > method, but what does it actually release?
                > >
                > > I would basically prefer to skip invoking Dispose() as this will free me
                > > from determining when the usage actually has finished.
                > >
                > >
                > > Best regards,
                > >
                > > Henrik Dahl
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Angel Saenz-Badillos[MS]

                  #9
                  Re: Should I Dispose() an SqlCommand object?

                  The reason that the samples in the microsoft documentation do not show calls
                  to Dispose for the SqlCommand object is that, as it has been stated in this
                  thread, this does nothing. Looking at the ildasm of SqlCommand shows that it
                  does not override dispose and just inherits it from the base. The person
                  writing the sample knew this and coded accordingly. I have written some of
                  the examples in the docs myself and have done the same thing.

                  The problem is that as the next version of the urt rolls along there are no
                  guarantees that dispose will keep doing nothing, it is also tedious to
                  figure out exactly what objects you have to dispose at all costs
                  (SqlConnection! ) and which you can ignore completelly. The general rule of
                  "if it implements idisposable, dispose it" will always work.

                  I am familiar with the Quote you mention. This information was based on
                  outdated information, for Beta 1 the SqlConnection did not implement a
                  completelly managed pooling solution, instead it relied on Enterprise
                  services object pooling. There where a number of problems with this
                  implementation but it was always meant to be temporary, the documentation
                  was added to the Beta documentation to work arround some issues with Dispose
                  and it was only fixed by v1.1! (auch, my fault)

                  Calling Dispose on the SqlConnection does not close the connection to sql
                  server, the only thing that Dispose does internally is to set the connection
                  string to null and to call Close if the connection has not already been
                  closed. The recommended usage (for the same reasons as I am recomending
                  always calling dispose) is to call using Sqlconnection (which automatically
                  disposes) and to call connection.Clos e(), there is no perf penalty for
                  calling both.

                  Hope this helps
                  --
                  Angel Saenz-Badillos [MS] Managed Providers
                  This posting is provided "AS IS", with no warranties, and confers no rights.
                  Please do not send email directly to this alias. This alias is for newsgroup
                  purposes only.
                  "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                  news:OQggL0QjDH A.2544@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > Angel,
                  >
                  > Yes, that's obvious, but how may it then be that there are many, many
                  > examples from Microsoft which do not invoke the Dispose() method of
                  > SqlCommand objects, even examples which include Disposing e.g. the
                  > SqlConnection object?
                  >
                  > A question:
                  > In one of the books from Microsoft Press it's stressed that you should
                  > Close(), not Dispose(), an SqlConnection object because Dispose() will[/color]
                  close[color=blue]
                  > the connection to MS SQL Server whereas Close() lets the connection to go
                  > back to the pool, i.e. closes the logical connection. In the ".NET Data
                  > Access Architecture Guide" it's claimed that both Close() and Dispose()[/color]
                  let[color=blue]
                  > the SqlConnection to go back to the pool. Which is correct?
                  >
                  >
                  > Best regards,
                  >
                  > Henrik Dahl
                  >
                  > "Angel Saenz-Badillos[MS]" <angelsa@online .microsoft.com> wrote in message
                  > news:uvLde$PjDH A.1964@TK2MSFTN GP12.phx.gbl...[color=green]
                  > > You should call Dispose on any object that implements IDisposable, this[/color][/color]
                  is[color=blue][color=green]
                  > > very easy to do with C# and the "using" keyword, in other .net languages[/color]
                  > you[color=green]
                  > > can use the try finally construct to ensure that Dispose gets called. In
                  > > general adhering to this basic principle will greatly reduce stress[/color]
                  > related[color=green]
                  > > problems with your code and may increase performance as it makes[/color][/color]
                  cleaning[color=blue]
                  > up[color=green]
                  > > smarter.
                  > >
                  > > Something else to think about is that allthough currently the Command[/color][/color]
                  may[color=blue][color=green]
                  > > not be doing anything meaningfull in its dispose method, there is no
                  > > guarantee that this will be true for future releases of the framework.
                  > > Calling dispose for all disposable objects will greatly enhance the[/color]
                  > lifetime[color=green]
                  > > of your code.
                  > >
                  > > Hope this helped,
                  > > --
                  > > Angel Saenz-Badillos [MS] Managed Providers
                  > > This posting is provided "AS IS", with no warranties, and confers no[/color]
                  > rights.[color=green]
                  > > Please do not send email directly to this alias. This alias is for[/color]
                  > newsgroup[color=green]
                  > > purposes only.
                  > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                  > > news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...[color=darkred]
                  > > > Hello!
                  > > >
                  > > > After I've finished using an instance of the SqlCommand class, should[/color][/color][/color]
                  I[color=blue][color=green]
                  > > then[color=darkred]
                  > > > invoke Dispose() on the instance. I suppose so, as there is a Dispose
                  > > > method, but what does it actually release?
                  > > >
                  > > > I would basically prefer to skip invoking Dispose() as this will free[/color][/color][/color]
                  me[color=blue][color=green][color=darkred]
                  > > > from determining when the usage actually has finished.
                  > > >
                  > > >
                  > > > Best regards,
                  > > >
                  > > > Henrik Dahl
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Henrik Dahl

                    #10
                    Re: Should I Dispose() an SqlCommand object?

                    Angel,

                    Thank you very much for your precise answer.


                    Henrik Dahl

                    "Angel Saenz-Badillos[MS]" <angelsa@online .microsoft.com> wrote in message
                    news:OBUX6gRjDH A.2640@TK2MSFTN GP10.phx.gbl...[color=blue]
                    > The reason that the samples in the microsoft documentation do not show[/color]
                    calls[color=blue]
                    > to Dispose for the SqlCommand object is that, as it has been stated in[/color]
                    this[color=blue]
                    > thread, this does nothing. Looking at the ildasm of SqlCommand shows that[/color]
                    it[color=blue]
                    > does not override dispose and just inherits it from the base. The person
                    > writing the sample knew this and coded accordingly. I have written some of
                    > the examples in the docs myself and have done the same thing.
                    >
                    > The problem is that as the next version of the urt rolls along there are[/color]
                    no[color=blue]
                    > guarantees that dispose will keep doing nothing, it is also tedious to
                    > figure out exactly what objects you have to dispose at all costs
                    > (SqlConnection! ) and which you can ignore completelly. The general rule of
                    > "if it implements idisposable, dispose it" will always work.
                    >
                    > I am familiar with the Quote you mention. This information was based on
                    > outdated information, for Beta 1 the SqlConnection did not implement a
                    > completelly managed pooling solution, instead it relied on Enterprise
                    > services object pooling. There where a number of problems with this
                    > implementation but it was always meant to be temporary, the documentation
                    > was added to the Beta documentation to work arround some issues with[/color]
                    Dispose[color=blue]
                    > and it was only fixed by v1.1! (auch, my fault)
                    >
                    > Calling Dispose on the SqlConnection does not close the connection to sql
                    > server, the only thing that Dispose does internally is to set the[/color]
                    connection[color=blue]
                    > string to null and to call Close if the connection has not already been
                    > closed. The recommended usage (for the same reasons as I am recomending
                    > always calling dispose) is to call using Sqlconnection (which[/color]
                    automatically[color=blue]
                    > disposes) and to call connection.Clos e(), there is no perf penalty for
                    > calling both.
                    >
                    > Hope this helps
                    > --
                    > Angel Saenz-Badillos [MS] Managed Providers
                    > This posting is provided "AS IS", with no warranties, and confers no[/color]
                    rights.[color=blue]
                    > Please do not send email directly to this alias. This alias is for[/color]
                    newsgroup[color=blue]
                    > purposes only.
                    > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                    > news:OQggL0QjDH A.2544@TK2MSFTN GP11.phx.gbl...[color=green]
                    > > Angel,
                    > >
                    > > Yes, that's obvious, but how may it then be that there are many, many
                    > > examples from Microsoft which do not invoke the Dispose() method of
                    > > SqlCommand objects, even examples which include Disposing e.g. the
                    > > SqlConnection object?
                    > >
                    > > A question:
                    > > In one of the books from Microsoft Press it's stressed that you should
                    > > Close(), not Dispose(), an SqlConnection object because Dispose() will[/color]
                    > close[color=green]
                    > > the connection to MS SQL Server whereas Close() lets the connection to[/color][/color]
                    go[color=blue][color=green]
                    > > back to the pool, i.e. closes the logical connection. In the ".NET Data
                    > > Access Architecture Guide" it's claimed that both Close() and Dispose()[/color]
                    > let[color=green]
                    > > the SqlConnection to go back to the pool. Which is correct?
                    > >
                    > >
                    > > Best regards,
                    > >
                    > > Henrik Dahl
                    > >
                    > > "Angel Saenz-Badillos[MS]" <angelsa@online .microsoft.com> wrote in[/color][/color]
                    message[color=blue][color=green]
                    > > news:uvLde$PjDH A.1964@TK2MSFTN GP12.phx.gbl...[color=darkred]
                    > > > You should call Dispose on any object that implements IDisposable,[/color][/color][/color]
                    this[color=blue]
                    > is[color=green][color=darkred]
                    > > > very easy to do with C# and the "using" keyword, in other .net[/color][/color][/color]
                    languages[color=blue][color=green]
                    > > you[color=darkred]
                    > > > can use the try finally construct to ensure that Dispose gets called.[/color][/color][/color]
                    In[color=blue][color=green][color=darkred]
                    > > > general adhering to this basic principle will greatly reduce stress[/color]
                    > > related[color=darkred]
                    > > > problems with your code and may increase performance as it makes[/color][/color]
                    > cleaning[color=green]
                    > > up[color=darkred]
                    > > > smarter.
                    > > >
                    > > > Something else to think about is that allthough currently the Command[/color][/color]
                    > may[color=green][color=darkred]
                    > > > not be doing anything meaningfull in its dispose method, there is no
                    > > > guarantee that this will be true for future releases of the framework.
                    > > > Calling dispose for all disposable objects will greatly enhance the[/color]
                    > > lifetime[color=darkred]
                    > > > of your code.
                    > > >
                    > > > Hope this helped,
                    > > > --
                    > > > Angel Saenz-Badillos [MS] Managed Providers
                    > > > This posting is provided "AS IS", with no warranties, and confers no[/color]
                    > > rights.[color=darkred]
                    > > > Please do not send email directly to this alias. This alias is for[/color]
                    > > newsgroup[color=darkred]
                    > > > purposes only.
                    > > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                    > > > news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...
                    > > > > Hello!
                    > > > >
                    > > > > After I've finished using an instance of the SqlCommand class,[/color][/color][/color]
                    should[color=blue]
                    > I[color=green][color=darkred]
                    > > > then
                    > > > > invoke Dispose() on the instance. I suppose so, as there is a[/color][/color][/color]
                    Dispose[color=blue][color=green][color=darkred]
                    > > > > method, but what does it actually release?
                    > > > >
                    > > > > I would basically prefer to skip invoking Dispose() as this will[/color][/color][/color]
                    free[color=blue]
                    > me[color=green][color=darkred]
                    > > > > from determining when the usage actually has finished.
                    > > > >
                    > > > >
                    > > > > Best regards,
                    > > > >
                    > > > > Henrik Dahl
                    > > > >
                    > > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Angel Saenz-Badillos[MS]

                      #11
                      Re: Should I Dispose() an SqlCommand object?

                      I apologize if my answer seems brusque, I was trying to convey that
                      disposing Idisposable objects is always a good thing, it is by no means
                      necesary.

                      Objects that do not require dispose to be called on today should continue to
                      work as such for existing code, we will treat any changes in this behavior
                      as a bug. My hope is that we can avoid any such bugs going forward but
                      realistically there may be instances where this does not happen, there may
                      also be cases where new features that use an object that does not require
                      dispose today may require or benefit from the object being disposed in the
                      future, this may make things even more confusing.

                      I guess my question is what kind of examples would you be interested to see
                      in the documentation that ships with the next version of the URT? How do you
                      rate your experience with the current batch of samples and how much would it
                      help if they were to be rewritten? One of the biggest problems with samples
                      is that VB.NET does not support the "using" keyword, so equivalent C# and
                      VB.NET samples become less clear, would this be a concern when looking at
                      doc samples?

                      looking forward to any doc related feedback
                      Thanks,
                      --
                      Angel Saenz-Badillos [MS] Managed Providers
                      This posting is provided "AS IS", with no warranties, and confers no rights.
                      Please do not send email directly to this alias. This alias is for newsgroup
                      purposes only.
                      "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                      news:uvtTypijDH A.1764@tk2msftn gp13.phx.gbl...[color=blue]
                      > Angel,
                      >
                      > Thank you very much for your precise answer.
                      >
                      >
                      > Henrik Dahl
                      >
                      > "Angel Saenz-Badillos[MS]" <angelsa@online .microsoft.com> wrote in message
                      > news:OBUX6gRjDH A.2640@TK2MSFTN GP10.phx.gbl...[color=green]
                      > > The reason that the samples in the microsoft documentation do not show[/color]
                      > calls[color=green]
                      > > to Dispose for the SqlCommand object is that, as it has been stated in[/color]
                      > this[color=green]
                      > > thread, this does nothing. Looking at the ildasm of SqlCommand shows[/color][/color]
                      that[color=blue]
                      > it[color=green]
                      > > does not override dispose and just inherits it from the base. The person
                      > > writing the sample knew this and coded accordingly. I have written some[/color][/color]
                      of[color=blue][color=green]
                      > > the examples in the docs myself and have done the same thing.
                      > >
                      > > The problem is that as the next version of the urt rolls along there are[/color]
                      > no[color=green]
                      > > guarantees that dispose will keep doing nothing, it is also tedious to
                      > > figure out exactly what objects you have to dispose at all costs
                      > > (SqlConnection! ) and which you can ignore completelly. The general rule[/color][/color]
                      of[color=blue][color=green]
                      > > "if it implements idisposable, dispose it" will always work.
                      > >
                      > > I am familiar with the Quote you mention. This information was based on
                      > > outdated information, for Beta 1 the SqlConnection did not implement a
                      > > completelly managed pooling solution, instead it relied on Enterprise
                      > > services object pooling. There where a number of problems with this
                      > > implementation but it was always meant to be temporary, the[/color][/color]
                      documentation[color=blue][color=green]
                      > > was added to the Beta documentation to work arround some issues with[/color]
                      > Dispose[color=green]
                      > > and it was only fixed by v1.1! (auch, my fault)
                      > >
                      > > Calling Dispose on the SqlConnection does not close the connection to[/color][/color]
                      sql[color=blue][color=green]
                      > > server, the only thing that Dispose does internally is to set the[/color]
                      > connection[color=green]
                      > > string to null and to call Close if the connection has not already been
                      > > closed. The recommended usage (for the same reasons as I am recomending
                      > > always calling dispose) is to call using Sqlconnection (which[/color]
                      > automatically[color=green]
                      > > disposes) and to call connection.Clos e(), there is no perf penalty for
                      > > calling both.
                      > >
                      > > Hope this helps
                      > > --
                      > > Angel Saenz-Badillos [MS] Managed Providers
                      > > This posting is provided "AS IS", with no warranties, and confers no[/color]
                      > rights.[color=green]
                      > > Please do not send email directly to this alias. This alias is for[/color]
                      > newsgroup[color=green]
                      > > purposes only.
                      > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in message
                      > > news:OQggL0QjDH A.2544@TK2MSFTN GP11.phx.gbl...[color=darkred]
                      > > > Angel,
                      > > >
                      > > > Yes, that's obvious, but how may it then be that there are many, many
                      > > > examples from Microsoft which do not invoke the Dispose() method of
                      > > > SqlCommand objects, even examples which include Disposing e.g. the
                      > > > SqlConnection object?
                      > > >
                      > > > A question:
                      > > > In one of the books from Microsoft Press it's stressed that you should
                      > > > Close(), not Dispose(), an SqlConnection object because Dispose() will[/color]
                      > > close[color=darkred]
                      > > > the connection to MS SQL Server whereas Close() lets the connection to[/color][/color]
                      > go[color=green][color=darkred]
                      > > > back to the pool, i.e. closes the logical connection. In the ".NET[/color][/color][/color]
                      Data[color=blue][color=green][color=darkred]
                      > > > Access Architecture Guide" it's claimed that both Close() and[/color][/color][/color]
                      Dispose()[color=blue][color=green]
                      > > let[color=darkred]
                      > > > the SqlConnection to go back to the pool. Which is correct?
                      > > >
                      > > >
                      > > > Best regards,
                      > > >
                      > > > Henrik Dahl
                      > > >
                      > > > "Angel Saenz-Badillos[MS]" <angelsa@online .microsoft.com> wrote in[/color][/color]
                      > message[color=green][color=darkred]
                      > > > news:uvLde$PjDH A.1964@TK2MSFTN GP12.phx.gbl...
                      > > > > You should call Dispose on any object that implements IDisposable,[/color][/color]
                      > this[color=green]
                      > > is[color=darkred]
                      > > > > very easy to do with C# and the "using" keyword, in other .net[/color][/color]
                      > languages[color=green][color=darkred]
                      > > > you
                      > > > > can use the try finally construct to ensure that Dispose gets[/color][/color][/color]
                      called.[color=blue]
                      > In[color=green][color=darkred]
                      > > > > general adhering to this basic principle will greatly reduce stress
                      > > > related
                      > > > > problems with your code and may increase performance as it makes[/color]
                      > > cleaning[color=darkred]
                      > > > up
                      > > > > smarter.
                      > > > >
                      > > > > Something else to think about is that allthough currently the[/color][/color][/color]
                      Command[color=blue][color=green]
                      > > may[color=darkred]
                      > > > > not be doing anything meaningfull in its dispose method, there is no
                      > > > > guarantee that this will be true for future releases of the[/color][/color][/color]
                      framework.[color=blue][color=green][color=darkred]
                      > > > > Calling dispose for all disposable objects will greatly enhance the
                      > > > lifetime
                      > > > > of your code.
                      > > > >
                      > > > > Hope this helped,
                      > > > > --
                      > > > > Angel Saenz-Badillos [MS] Managed Providers
                      > > > > This posting is provided "AS IS", with no warranties, and confers no
                      > > > rights.
                      > > > > Please do not send email directly to this alias. This alias is for
                      > > > newsgroup
                      > > > > purposes only.
                      > > > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote in[/color][/color][/color]
                      message[color=blue][color=green][color=darkred]
                      > > > > news:evi$Pj$iDH A.548@TK2MSFTNG P11.phx.gbl...
                      > > > > > Hello!
                      > > > > >
                      > > > > > After I've finished using an instance of the SqlCommand class,[/color][/color]
                      > should[color=green]
                      > > I[color=darkred]
                      > > > > then
                      > > > > > invoke Dispose() on the instance. I suppose so, as there is a[/color][/color]
                      > Dispose[color=green][color=darkred]
                      > > > > > method, but what does it actually release?
                      > > > > >
                      > > > > > I would basically prefer to skip invoking Dispose() as this will[/color][/color]
                      > free[color=green]
                      > > me[color=darkred]
                      > > > > > from determining when the usage actually has finished.
                      > > > > >
                      > > > > >
                      > > > > > Best regards,
                      > > > > >
                      > > > > > Henrik Dahl
                      > > > > >
                      > > > > >
                      > > > >
                      > > > >
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      Working...