0

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);
        }
    }
}
jeremy.o
  • 73
  • 5
  • 8
    Think of it as opposite but the same as what you said. The `using` clause disposes of the connection object, which in turn closes the connection. – Lasse V. Karlsen Feb 03 '22 at 11:41
  • 1
    Does this answer your question? [What is the C# Using block and why should I use it?](https://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it) – Charlieface Feb 03 '22 at 14:30

0 Answers0