By "using statements," I assume that you mean something like below?
Yes, only I would also use them on the command and adapter objects
(IMO you should use them on any object the implements the IDisposable
interface (has a dispose method)).
This works, but I am not yet certain that I understand the cleanup issue and
when I do and don't need to consider cleanup.
**Note - I'm not trying to start another Dispose/Don't Dispose war
here**
Consider the following:
Public Sub Foo()
Dim connection as new OleDbConnection(connString)
Dim com as OleDbCommand = conn.CreateCommand()
com.commandtext = "Select * From SomeWhere"
com.ExecuteNonQuery()
End Sub
As is, that procedure will create two objects(a connection and command
object) and use them. When the procedure exits, both of these objects
will go out of scope and be marked for collection. Eventually, the
garbage collector (the GC) will execute it's collect method and "spot"
these two objects and will call their Finalize methods. The Finalize
methods will call the objects Dispose method which will do the actual
clean up of resources it uses. After having the Finalize method called
by the GC, the object will be collected during the next garbage
collection cycle. During the time between the objects going out of
scope and being collected by the GC, the objects are needlessly using
resources.
So we can change the code to this and use Using statements to manually
dispose of the objects:
Public Sub Foo()
Dim conn as new OleDbConnection(connString)
Using (conn)
Dim com as OleDbCommand = conn.CreateCommand()
Using (com)
com.commandtext = "Select * From SomeWhere"
com.ExecuteNonQuery()
End Using
End Using
End Sub
This way, as soon as the using statements finish the objects will
dispose of themselves and clean up any resources they uses, as well as
suppressing their finalize methods. The first benefit of this is that
the object is no longer needlessly holding on to other resources while
it waits to go through the garbage collection process. The second
benefit is that when the GC executes, it will see that the objects
don't need to have there Finalize methods called (because of the
suppressfinialize call in the dispose method), and will immediately
collect the object, instead of having to wait until the next
collection cycle to do so.
I hope I made sense there, I was typing awfully fast. If you have any
more questions please ask and I'll try to answer them!
Thanks,
Seth Rowe
On Apr 5, 9:35 am, "Jeff" <n...@nothingX.comwrote:
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
...I left out the disposing statements for simplicity's sake
(though you should dispose of these objects to ensure fast cleanup) I
usually wrap the usage of each object in using statements.
Also, is there a reason you want to dump a single row into a
datatable? If you just want to retrieve that value you could also use
a DataReader instead of the DataAdapter/DataTable pair.
Seth Rowe
It worked. Thanks.
By "using statements," I assume that you mean something like below?
This works, but I am not yet certain that I understand the cleanup issue and
when I do and don't need to consider cleanup.
I am using dataAdapter/DataTable for a single row only because the first
code that I learned required an entire table and I don't yet have an example
for a single row (i.e., the WWWTable below contains multiple rows).
Everytime I write something like this, Dim sss As New DataRow , I get an
error about sss being "protected" and can't go further. Would you be so kind
as to provide an example or give me a hint about what might be wrong?
Jeff
WWWTable = New DataTable
UUUTable = New DataTable
Using conn As New OdbcConnection(MyConnString)
Dim adapter As OdbcDataAdapter = New OdbcDataAdapter("call
XXX", conn)
adapter.Fill(WWWTable)
Dim command As New OdbcCommand("call YYY(?)", conn)
command.Parameters.Add("User", OdbcType.VarChar).Value =
Session("User")
Dim da As New OdbcDataAdapter(command)
da.Fill(UUUTable)
End Using
--
Posted via a free Usenet account fromhttp://www.teranews.com