Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SqlConnection

Posted on 2011-03-07
2
Medium Priority
?
319 Views
Last Modified: 2012-05-11
Pls see code attached.
Do I need to write cnn.Close() and  cnn.Dispose() statements in that code or the statement
using (var cnn = new SqlConnection(MyConnection)){}

Open in new window

takes care of it automatically?

Thanks.
using (var cnn = new SqlConnection(MyConnection))
        {
            using (var cmd = new SqlCommand("[mySchema].[UpdateComments]", cnn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@comment", comment);
               cnn.Open();
                cmd.ExecuteNonQuery();
            }
            cnn.Close();
            cnn.Dispose();
        }

Open in new window

0
Comment
Question by:patd1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
lenordiste earned 2000 total points
ID: 35063111
"using" will call dispose on your SqlConnection object and thus close the connection. You don't need to write cnn.Close() or cnn.Dispose()
0
 
LVL 11

Assisted Solution

by:lenordiste
lenordiste earned 2000 total points
ID: 35063124
i would rewrite your code like so:
using (var cnn = new SqlConnection(MyConnection))
        {
            using (var cmd = new SqlCommand("[mySchema].[UpdateComments]", cnn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@comment", comment);
               cnn.Open();
                cmd.ExecuteNonQuery();
            }
        }

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question