Dispose Method question

B

bozzzza

If I did something like this :-

=======================================================================================
//Method to return a ref to an SqlConnection
private SqlConnection GetConnection()
{
SqlConnection conn = new SqlConnection(Globals.cCONN_STR);
return conn;
}

//Get the SubScription Info and Return the structure
protected void TestFunction()
{
string sqlstmt = "select * from test";
using (SqlCommand command = new SqlCommand(sqlstmt,GetConnection()))
{
//Do Something Here
}
}
========================================================================================

Because I have used the using statement the command object will call
its dispose method when it leaves the using scope.

But I was wondering what happens to the SqlConnection object in the
TestFunction(): does it get disposed when the command object gets
disposed?

Or would I have to do something like this before it left the using
block: command.Connection.Dispose()?


TIA

Simon
 
T

Tim Haughton

But I was wondering what happens to the SqlConnection object in the
TestFunction(): does it get disposed when the command object gets
disposed?

Or would I have to do something like this before it left the using
block: command.Connection.Dispose()?

You would have to manually call dispose somewhere. It might be nice if the
using statement could hold multiple objects of different types, I'm sure
someone knows a reason why it can hold only multiple objects of the same
type.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
C

Cor Ligthert [MVP]

Simon,

Every managed object that has no reference to something or from something
will be released sooner or later by the GC. If it is disposed or not is
disposed.

(And because that the dataadapter is released, will the connection be that
as well).

I hope this helps,

Cor
 
C

Christof Nordiek

If I did something like this :-

=======================================================================================
//Method to return a ref to an SqlConnection
private SqlConnection GetConnection()
{
SqlConnection conn = new SqlConnection(Globals.cCONN_STR);
return conn;
}

//Get the SubScription Info and Return the structure
protected void TestFunction()
{
string sqlstmt = "select * from test";
using (SqlCommand command = new SqlCommand(sqlstmt,GetConnection()))
{
//Do Something Here
}
}
========================================================================================

Because I have used the using statement the command object will call
its dispose method when it leaves the using scope.

But I was wondering what happens to the SqlConnection object in the
TestFunction(): does it get disposed when the command object gets
disposed?

Or would I have to do something like this before it left the using
block: command.Connection.Dispose()?


TIA

Simon

It depends on, how you use the command object.
In mamy cases you have to mannully open and close the Connection.
Then you should use a using statement with the connection.Open() just before
it.

If you use ExecuteReader(CommandBehavior.CloseConnection);
Closing the DataReader will Close the connection.
So you could use a using statement on the reader.

Christof
 

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