We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

SQL Server C# using over try/catch block advatages

Charles Baldo
on
Medium Priority
1,478 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

Comment
Watch Question

Technical Manager
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Charles BaldoSoftware Developer

Author

Commented:
Thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.