|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
scope of command and garbage collectionSay I have the following piece of code
Dim dtPerson As New DataTable Dim cmd As New SqlClient.SqlCommand("sp_test", database) dtPerson.Load(cmd.ExecuteReader) cmd.dispose() which instanciates the command object, uses it then destroys it, now if i rewrote it like this Dim dtPerson As New DataTable dtPerson.Load(New SqlClient.SqlCommand("test", database) would that still act the same? because the commands scope is inside the load method, shouldnt it be destroyed after the point of execution has passed the load method? basicly creating a command that destroys itself after execution? or is it better not to do it this way because that is not how it works?
Show quote
Hide quote
"Smokey Grindle" <nospamhere@dontspam.net> schrieb: No, it won't. It will be destoyed by the garbage collector later, but there > Say I have the following piece of code > > Dim dtPerson As New DataTable > Dim cmd As New SqlClient.SqlCommand("sp_test", database) > dtPerson.Load(cmd.ExecuteReader) > cmd.dispose() > > which instanciates the command object, uses it then destroys it, now if i > rewrote it like this > > Dim dtPerson As New DataTable > dtPerson.Load(New SqlClient.SqlCommand("test", database) > > would that still act the same? because the commands scope is inside the > load method, shouldnt it be destroyed after the point of execution has > passed the load method? is no guarantee that it gets destroyed immediately. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Smokey,
Addition to Herfried's comments. In .NET 2.0 I would recommend: | Dim dtPerson As New DataTable Using cmd As New SqlClient.SqlCommand("sp_test", database)| dtPerson.Load(cmd.ExecuteReader) End UsingIf .NET 1.x I would recommend: | Dim dtPerson As New DataTable This ensures that the SqlCommand is disposed of even if the DataTable.Load | Dim cmd As New SqlClient.SqlCommand("sp_test", database) Try | dtPerson.Load(cmd.ExecuteReader) Finally | cmd.dispose() End Try method itself threw an exception. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Smokey Grindle" <nospamhere@dontspam.net> wrote in message news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl... | Say I have the following piece of code | | Dim dtPerson As New DataTable | Dim cmd As New SqlClient.SqlCommand("sp_test", database) | dtPerson.Load(cmd.ExecuteReader) | cmd.dispose() | | which instanciates the command object, uses it then destroys it, now if i | rewrote it like this | | Dim dtPerson As New DataTable | dtPerson.Load(New SqlClient.SqlCommand("test", database) | | would that still act the same? because the commands scope is inside the load | method, shouldnt it be destroyed after the point of execution has passed the | load method? basicly creating a command that destroys itself after | execution? or is it better not to do it this way because that is not how it | works? | | Yes, I was also looking at USING.
thanks both Show quoteHide quote "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in message news:eJEJTKzfGHA.2208@TK2MSFTNGP05.phx.gbl... > Smokey, > Addition to Herfried's comments. > > In .NET 2.0 I would recommend: > > | Dim dtPerson As New DataTable > Using cmd As New SqlClient.SqlCommand("sp_test", database) > | dtPerson.Load(cmd.ExecuteReader) > End Using > > If .NET 1.x I would recommend: > > | Dim dtPerson As New DataTable > | Dim cmd As New SqlClient.SqlCommand("sp_test", database) > Try > | dtPerson.Load(cmd.ExecuteReader) > Finally > | cmd.dispose() > End Try > > This ensures that the SqlCommand is disposed of even if the DataTable.Load > method itself threw an exception. > > -- > Hope this helps > Jay B. Harlow [MVP - Outlook] > .NET Application Architect, Enthusiast, & Evangelist > T.S. Bradley - http://www.tsbradley.net > > > "Smokey Grindle" <nospamhere@dontspam.net> wrote in message > news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl... > | Say I have the following piece of code > | > | Dim dtPerson As New DataTable > | Dim cmd As New SqlClient.SqlCommand("sp_test", database) > | dtPerson.Load(cmd.ExecuteReader) > | cmd.dispose() > | > | which instanciates the command object, uses it then destroys it, now if > i > | rewrote it like this > | > | Dim dtPerson As New DataTable > | dtPerson.Load(New SqlClient.SqlCommand("test", database) > | > | would that still act the same? because the commands scope is inside the > load > | method, shouldnt it be destroyed after the point of execution has passed > the > | load method? basicly creating a command that destroys itself after > | execution? or is it better not to do it this way because that is not how > it > | works? > | > | > > Hi Jay,
Can you explain a little more about the difference between the Using in .NET 2.0 and the .NET 1.1 version of the code? I am new to .NET 2.0, but quite experienced with .NET 1.1. I had mostly used the Try/Catch method, but I'm thinking .NET 2.0 wraps a hidden exception handler around the Using statement for just such eventualities. Is there an MSDN document about this? Thanks, Robin Show quoteHide quote "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in message news:eJEJTKzfGHA.2208@TK2MSFTNGP05.phx.gbl... > Smokey, > Addition to Herfried's comments. > > In .NET 2.0 I would recommend: > > | Dim dtPerson As New DataTable > Using cmd As New SqlClient.SqlCommand("sp_test", database) > | dtPerson.Load(cmd.ExecuteReader) > End Using > > If .NET 1.x I would recommend: > > | Dim dtPerson As New DataTable > | Dim cmd As New SqlClient.SqlCommand("sp_test", database) > Try > | dtPerson.Load(cmd.ExecuteReader) > Finally > | cmd.dispose() > End Try > > This ensures that the SqlCommand is disposed of even if the DataTable.Load > method itself threw an exception. > > -- > Hope this helps > Jay B. Harlow [MVP - Outlook] > .NET Application Architect, Enthusiast, & Evangelist > T.S. Bradley - http://www.tsbradley.net > > > "Smokey Grindle" <nospamhere@dontspam.net> wrote in message > news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl... > | Say I have the following piece of code > | > | Dim dtPerson As New DataTable > | Dim cmd As New SqlClient.SqlCommand("sp_test", database) > | dtPerson.Load(cmd.ExecuteReader) > | cmd.dispose() > | > | which instanciates the command object, uses it then destroys it, now if > i > | rewrote it like this > | > | Dim dtPerson As New DataTable > | dtPerson.Load(New SqlClient.SqlCommand("test", database) > | > | would that still act the same? because the commands scope is inside the > load > | method, shouldnt it be destroyed after the point of execution has passed > the > | load method? basicly creating a command that destroys itself after > | execution? or is it better not to do it this way because that is not how > it > | works? > | > | > > Robin,
The Using statement is effectively: Dim dtPerson As New DataTable Using cmd As New SqlClient.SqlCommand("sp_test", database) + Dim cmd As New SqlClient.SqlCommand("sp_test", database) + Try dtPerson.Load(cmd.ExecuteReader) End Using + Finally + If cmd IsNot Nothing + cmd.dispose() + End If +End Try Where the lines marked with + are what VB creates in the IL. You can use ILDASM to see specifically what VB does. The IsNot Nothing is there to allow for the case where cmd is set to nothing within the Using block. For details of the new statement see: http://msdn2.microsoft.com/en-US/library/htd05whh.aspx -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Robin Mark Tucker" <robintuckerhome@removehotmail.comremove> wrote in message news:e51m7d$jl1$1$8302bc10@news.demon.co.uk... | | Hi Jay, | | Can you explain a little more about the difference between the Using in ..NET | 2.0 and the .NET 1.1 version of the code? I am new to .NET 2.0, but quite | experienced with .NET 1.1. I had mostly used the Try/Catch method, but I'm | thinking .NET 2.0 wraps a hidden exception handler around the Using | statement for just such eventualities. Is there an MSDN document about | this? | | Thanks, | | | Robin | | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in | message news:eJEJTKzfGHA.2208@TK2MSFTNGP05.phx.gbl... | > Smokey, | > Addition to Herfried's comments. | > | > In .NET 2.0 I would recommend: | > | > | Dim dtPerson As New DataTable | > Using cmd As New SqlClient.SqlCommand("sp_test", database) | > | dtPerson.Load(cmd.ExecuteReader) | > End Using | > | > If .NET 1.x I would recommend: | > | > | Dim dtPerson As New DataTable | > | Dim cmd As New SqlClient.SqlCommand("sp_test", database) | > Try | > | dtPerson.Load(cmd.ExecuteReader) | > Finally | > | cmd.dispose() | > End Try | > | > This ensures that the SqlCommand is disposed of even if the DataTable.Load | > method itself threw an exception. | > | > -- | > Hope this helps | > Jay B. Harlow [MVP - Outlook] | > .NET Application Architect, Enthusiast, & Evangelist | > T.S. Bradley - http://www.tsbradley.net | > | > | > "Smokey Grindle" <nospamhere@dontspam.net> wrote in message | > news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl... | > | Say I have the following piece of code | > | | > | Dim dtPerson As New DataTable | > | Dim cmd As New SqlClient.SqlCommand("sp_test", database) | > | dtPerson.Load(cmd.ExecuteReader) | > | cmd.dispose() | > | | > | which instanciates the command object, uses it then destroys it, now if | > i | > | rewrote it like this | > | | > | Dim dtPerson As New DataTable | > | dtPerson.Load(New SqlClient.SqlCommand("test", database) | > | | > | would that still act the same? because the commands scope is inside the | > load | > | method, shouldnt it be destroyed after the point of execution has passed | > the | > | load method? basicly creating a command that destroys itself after | > | execution? or is it better not to do it this way because that is not how | > it | > | works? | > | | > | | > | > | | Jay,
I would not use "using" without global exception handling or without forcing the errors to the datarow.rowerror property. I know that you use probably the first and I the last, however in my opinion is it good to explain that. Cor Cor,
Agree, using (no pun intended) a global exception handler is a good idea along with appropriate "local" error handling if appropriate. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:%23GVZjO1fGHA.4940@TK2MSFTNGP05.phx.gbl... | Jay, | | I would not use "using" without global exception handling or without forcing | the errors to the datarow.rowerror property. | | I know that you use probably the first and I the last, however in my opinion | is it good to explain that. | | Cor | |
Xcopy Install of VB Classic Application
DataRelation vs DataView.RowFilter property Creating a "Back" button in VB.Net/ASP.Net Project won't run from network drive. How to Use a Screen Saver app within My Application? Applying word styles using VB .Net using up too much memory and is slow application.exit( ) Comparing Two DataTables SET ARITHABORT ON: Why (not)? Web Service |
|||||||||||||||||||||||