IS it required.... ?

A

Allen Smith

Is it required to destroy the command object(SqlCommand) by calling the
exclusivily the following statement when I am closing the associated
database connection all the time.
MySqlCommand = Nothing.

I am using .Net Framework 1.1. and SQL 2K.

Thank you. Any comments.

Any document to support your argument.

Allen
 
E

Earl

I've never had reason to destroy the command object. Of course, in my code,
it goes out of scope within the procedure or function where it's called.
 
S

Sahil Malik [MVP]

Okay first of all, since the SqlCommand object may hold a reference to a
connection, you need to make sure that your connection is closed.
Now once the connection itself is closed, then the statement below, and the
SqlCommand object falling out of scope will have the same effect i.e. they
will continue to take up a little bit memory until the GC comes and gets
them.

So is it necessary? Nope. Is it a good idea? A little bit :) (Depends on
how long you have your objects lingering around in scope).

HTH

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
M

Miha Markic [MVP C#]

Simply put: a good practise is to Dispose everything that implements
IDisposable interface without exceptions. Even if Dispose on certain
instance does nothing at the moment there are no guarantees that the Dispose
behavious won't change.
 
C

Cor Ligthert [MVP]

Allen,

As Earl in fact already statet, it can have sence when you have set your
SQLcommand global. That will give it the GC a change to release the
resources. Otherwise I keep it with Earls answer in those cases nothing can
make it earlier to be released than that all references to or from it are
(forced) set to nothing.

By instance in this code
\\\
private mysub
dim conn as new sqlconnection("theconnectionstring")
dim cmd as new sqlcommand("TheSqlstring",conn)
conn.open
messagebox.show(cmd.executescalar.toString)
conn.close
end sub
///

Everything goes out of scope so doing more makes no sence

Cor
 
M

Miha Markic [MVP C#]

Heya Cor,

Actually your code has a problem. Big one.
You didn't embed
messagebox.show(cmd.executescalar.toString)
into try/finally block. So, if exception happens the connection won't be
closed ever (until GC runs on the lost connection instance).
Cccc :)
(I know you know, but perhpas others do not realize it ;-))

conn.Open()
try
messagebox.show(cmd.executescalar.toString)
finally
conn.Close()
end try

Besides that who gurantees you that SqlCommand won't need manual Dispose,
perhaps in .net 2.0 or .net 2.1 (the same goes for all the others
IDisposable classes)?

So, I would do it like:

dim conn as new sqlconnection("theconnectionstring")
try
dim cmd as new sqlcommand("TheSqlstring",conn)
try
conn.Open()
messagebox.show(cmd.executescalar.toString)
finally
cmd.Dispose()
end try
finally
conn.Dispose()
end try

Perhaps it is just me :)
 
C

Cor Ligthert [MVP]

Miha,

That try block was something I had to decide to insert it, I left it out to
keep it short. You are right in that, it should be there.

About the dispose (you should seen the long and endless discussions about
that in the dotnet General newsgroup)
Perhaps it is just me :)

Yes

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top