thinkinterview » interview questions » .NET
« Previous

.NET Interview Questions

next »
question

In asp.net 2.0 what is the best way to handle sql connection state?

Answer Description:

As per the standard guideline connection object should be dispose in any case so that dangling connection object should not lie in memory. Connection object is handling SQL server connection which can not be disposing by .NET garbage collector.
To achieve this behavior we should always use try finally block which handling connection object and make sure to calling dispose method of connection object into finally block.

System.Data.SqlClient.SqlConnection connection =

                new System.Data.SqlClient.SqlConnection();

            try
            {
                connection.Open();

                //do some work

            }
            finally
            {
                connection.Dispose();
            }
There is a “using” statement in .net framework which is a shortcut of try finally block.

using ( System.Data.SqlClient.SqlConnection connection =

                new System.Data.SqlClient.SqlConnection() )

            {
                connection.Open();
 

                //do some work

 

                //no need to call connection.Dispose()

            }

 

 


Next Queston » What’s the difference between Codebehind="MyCode.aspx....
What’s the difference between Codebehind="MyCode.aspx.cs" and src="MyCode.aspx.cs" mce_src="MyCode.aspx.cs"... View full queston »