?
Solved

SQL Server C# using over try/catch block advatages

Posted on 2009-02-11
2
Medium Priority
?
1,391 Views
Last Modified: 2012-05-06
Hello,
                  
I am performing some SQL procedures in C#.  I am wondering what is the best approach as far as memory and frameworks management.
I have two approaches  using() and try Catch finally.  My questions are what does using() have encapsulated in it to dispose
of connections, commands, readers etc. Am I just as good using try catch annd cleaning up and disposing of my object smyself?
is the only hit on performance codeing size or is there other advantages using using() over other methods?

I have attached some code snippets. If anyone has any suggestions to improve performance, readablility I would appreciate it.
ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MetaData;Integrated Security = SSPI;";
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("select aFileName from dbo.asset_Table", conn))
                {
                    SqlDataReader objRead;
                    conn.Open();
                    objRead = cmd.ExecuteReader();
                    while (objRead.Read())
                    {
                        Console.WriteLine(objRead["aFileName"]);
                    }
                }
                conn.Close();
            }
			
************************ Try Catch finally method
			
            string ConnectionString;
 
            ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MetaData;Integrated Security = SSPI;";
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand("select aFileName from dbo.asset_Table", conn);
            SqlDataReader objRead;
 
            try
            {
                conn.Open();
                objRead = cmd.ExecuteReader();
                while (objRead.Read())
                {
                    Console.WriteLine(objRead["aFileName"]);
                }
                objRead.Close();
                objRead.Dispose();
 
            }
            catch (SqlException sqlEx)
            {
                Console.WriteLine("SqlException Handle :{0}", sqlEx.ToString());
            }
            finally
            {   
                conn.Close();
                conn.Dispose();
                cmd.Dispose();
            }

Open in new window

0
Comment
Question by:Charles Baldo
2 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 23613224
using blocks are used for object life time management as as soon as you exit your  using block the object goes out of scope and are placed on the dispose close
if you dont use the sqlconnection object in the using block then you have to specifically call the connection.close method to close the database connection where as in case of using it will be closed automatically as soon as the using block is over

try catch are normally used for catching the exceptions and they should be used in the using blocks as well because exceptions can happen there too
0
 

Author Closing Comment

by:Charles Baldo
ID: 31545632
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 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