PREAMBLE
I understand that normally most SQL calls run thusly
using(var cnn = new DbConnection("YOURCONNECTIONSTRINGHERE")
{
cnn.Open(); //Open the connection.
using(var cmd = new DbCommand("YourSQL", cnn)
{
cmd.ExecuteScalar; //Or whatever type of execution you want.
}
}
This will properly dispose of both the connection and the command.
My Question: Will this code properly dispose of both objects?
using(var cmd = new SqlCommand("YourSQL", new Connection("YOURCONNECTIONSTRINGHERE"))
{
cmd.ExecuteScalar; //Or whatever type of execution you want.
}
In reality I'm using a method that provides and opens the connection.
public SqlConnection Connection()
{
var product = new SQLConnection("ConnectionString");
product.Open();
return product;
}
So at the end of the day the call looks like this:
using(var cmd = new SqlCommand("YourSQL", Connection())
{
cmd.ExecuteScalar; //Or whatever type of execution you want.
}
I know the SqlCommand object will be disposed of but will the SQLConnection, created within the using parameter declaration, be disposed of? I've tried running some simple unit tests but it seems inconclusive.