I am using a using statement for database crud operations. I know the using statement helps close connections , but does it also dispose the connection object after it has been used? Here is a snippet of my code:
public void AdoInsertQuote(Quote quote)
{
try
{
using (var connection = new SqlConnection(Helper.CnnVal("QuotesDB")))
{
connection.Open();
var sql = "INSERT INTO Quotes (Author, Title, Description, Type, CreatedAt) VALUES (@Author, @Title, @Description, @Type, @CreatedAt)";
var sqlCommand = new SqlCommand(sql, connection);
sqlCommand.Parameters.AddWithValue("@Author", quote.Author);
sqlCommand.Parameters.AddWithValue("@Title", quote.Title);
sqlCommand.Parameters.AddWithValue("@Description", quote.Description);
sqlCommand.Parameters.AddWithValue("@Type", quote.Type);
sqlCommand.Parameters.AddWithValue("@CreatedAt", quote.CreatedAt);
var result = sqlCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}