By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

Does "using" automatically close SqlConnection

P: n/a

using (
SqlCommand cmd = new SqlCommand("select * sample"),
new SqlConnection( sqlDsn )
)
)

{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
}

Does the SqlConnection automatically get closed when I exit the using?
Feb 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In your specific scenario the SqlConnection will be closed but not by the
using statement. The SqlCommand will close the SqlConnection when it is
disposed by the using statement.
The SqlCommand will only close the SqlConnection if it opened the connection
itself. So if the connection was already open before it was passed as a
parameter to the SqlCommand it will not be closed by the SqlCommand.

Sandor

"John Bailo" <ja*****@texeme.com> wrote in message
news:_N********************@speakeasy.net...

using (
SqlCommand cmd = new SqlCommand("select * sample"),
new SqlConnection( sqlDsn )
)
)

{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
}

Does the SqlConnection automatically get closed when I exit the using?

Feb 21 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.