Yet Another using-dispose-connection-command Question.

N

NJC©\(TM\)

Hello Newsgroup,

In a typical way to execute a function performing a database operation (either
calling ExecuteScalar, ExecuteNonQuery, or ExecuteReader) referencing
System.Data.sqlClient, I am currently coding my functions as such;

using(sqlConnection sql_conn = new sqlConnection(conn_string))
{
using(sqlCommand sql_comm = CreateCommand(commandText, sql_conn))
{
conn.Open();
// Execute Function

} // sqlCommand

} // sqlConnection

Disposal of the Connection and Command is explicit.

What about (calling a custom Command creator function 'CreateCommand');

using(sqlCommand sql_comm = CreateCommand(commandText, new
sqlConnection(conn_string)))
{
sql_comm.Connection.Open();
// Execute Function

} // sqlCommand


Does the Connection get Disposed of, excepting sqlDataReader will close the
Connection if it's flagged to do so?

Does the Connection require explicit disposing in this case?
That is, 'sql_comm.Connection.Dispose();'

When Googling, does sqlCommand.Dispose() dispose sqlConnection, there are many
pages describing it the other way round, Connection disposing Commands.

Thanks and regards Newsgroup,
 
A

Arne Vajhøj

NJC©(TM) said:
Hello Newsgroup,

In a typical way to execute a function performing a database operation (either
calling ExecuteScalar, ExecuteNonQuery, or ExecuteReader) referencing
System.Data.sqlClient, I am currently coding my functions as such;

using(sqlConnection sql_conn = new sqlConnection(conn_string))
{
using(sqlCommand sql_comm = CreateCommand(commandText, sql_conn))
{
conn.Open();
// Execute Function

} // sqlCommand

} // sqlConnection

Disposal of the Connection and Command is explicit.

What about (calling a custom Command creator function 'CreateCommand');

using(sqlCommand sql_comm = CreateCommand(commandText, new
sqlConnection(conn_string)))
{
sql_comm.Connection.Open();
// Execute Function

} // sqlCommand


Does the Connection get Disposed of, excepting sqlDataReader will close the
Connection if it's flagged to do so?

Does the Connection require explicit disposing in this case?
That is, 'sql_comm.Connection.Dispose();'
Yes.

When Googling, does sqlCommand.Dispose() dispose sqlConnection, there are many
pages describing it the other way round, Connection disposing Commands.

I don't think it does. But check the .NET documentation not random
posts to usenet groups or blogs.

If the docs does not say that it does, then do not assume that it does.

Arne
 
B

Brian Gideon

Hello Newsgroup,

In a typical way to execute a function performing a database operation (either
calling ExecuteScalar, ExecuteNonQuery, or ExecuteReader) referencing
System.Data.sqlClient, I am currently coding my functions as such;

using(sqlConnection sql_conn = new sqlConnection(conn_string))
{
    using(sqlCommand sql_comm = CreateCommand(commandText, sql_conn))
    {
        conn.Open();
        // Execute Function

    } // sqlCommand

} // sqlConnection

Disposal of the Connection and Command is explicit.

What about (calling a custom Command creator function 'CreateCommand');

using(sqlCommand sql_comm = CreateCommand(commandText, new
sqlConnection(conn_string)))
{
    sql_comm.Connection.Open();
    // Execute Function

} // sqlCommand

Does the Connection get Disposed of, excepting sqlDataReader will close the
Connection if it's flagged to do so?

Like Peter I see no reference to a SqlDataReader so I cannot comment
on this question.
Does the Connection require explicit disposing in this case?
That is, 'sql_comm.Connection.Dispose();'

Yes. The general rule of thumb is that if an object owns its
IDisposable members then the object will dispose those members
automatically. In this case, since the SqlConnection is passed to the
SqlCommand through the constructor then it is assumed that SqlCommand
has no claim to ownership and thus it will not dispose the
SqlConnection automatically.
When Googling, does sqlCommand.Dispose() dispose sqlConnection, there aremany
pages describing it the other way round, Connection disposing Commands.

An IDbConnection disposing an IDbCommand makes a lot more sense than
the other way around. However, I still recommend that you call
Dispose (either explicitly or through 'using') on both. That way
you're not making any assumptions either way.
 
N

NJC©\(TM\)

Thank you all for your helpful responses.

|
| Hello Newsgroup,
|
| In a typical way to execute a function performing a database operation (either
| calling ExecuteScalar, ExecuteNonQuery, or ExecuteReader) referencing
| System.Data.sqlClient, I am currently coding my functions as such;
|
| using(sqlConnection sql_conn = new sqlConnection(conn_string))
| {
| using(sqlCommand sql_comm = CreateCommand(commandText, sql_conn))
| {
| conn.Open();
| // Execute Function
|
| } // sqlCommand
|
| } // sqlConnection
|
| Disposal of the Connection and Command is explicit.
|
| What about (calling a custom Command creator function 'CreateCommand');
|
| using(sqlCommand sql_comm = CreateCommand(commandText, new
| sqlConnection(conn_string)))
| {
| sql_comm.Connection.Open();
| // Execute Function
|
| } // sqlCommand
|
|
| Does the Connection get Disposed of, excepting sqlDataReader will close the
| Connection if it's flagged to do so?
|
| Does the Connection require explicit disposing in this case?
| That is, 'sql_comm.Connection.Dispose();'
|
| When Googling, does sqlCommand.Dispose() dispose sqlConnection, there are many
| pages describing it the other way round, Connection disposing Commands.
|
| Thanks and regards Newsgroup,
|
| --
| NJC©(TM)
|
|
|
|
|
 

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