Solved

SqlConnection

Posted on 2011-03-07
2
309 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
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
lenordiste earned 500 total points
Comment Utility
"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 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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 goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now