• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1404
  • Last Modified:

SQL Server C# using over try/catch block advatages

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
Charles Baldo
Asked:
Charles Baldo
1 Solution
 
Anurag ThakurTechnical ManagerCommented:
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
 
Charles BaldoSoftware DeveloperAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now