locked
how to close underlying DbConnection without using "Using" statement RRS feed

  • Question

  • User1270199477 posted

    Here's my  code,  I am using "Data Access Application Block"  in ObjectDataSource's class file.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    
    
    namespace cHospital
    {
        public class Hospital
        {
            private Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("Conn");
            public SqlDataReader sDR;
    
            public SqlDataReader GetHospitals()
            {
    
                RefCountingDataReader dr = (RefCountingDataReader)_db.ExecuteReader("GetHospitals");
                return (SqlDataReader)dr.InnerReader;
            }
    }
    }
    




    in my code, I didnt dispose the dataReader.  will it mess up my connection management ?  I want to make sure that DbConnection is properly closed.

    I found this from "hands on lab".
    "You are not doing any connection management here, but it is very important to Dispose the data reader returned. This is accomplished by the using statement in the code above. When the data reader is disposed, the underlying DbConnection is also closed."

    please let me know if what I did is ok, if not how do i close DbConnection properly.

    if you are wondering why I dont use "using" statement.

    basically, I bind my gridviews using ObjectDataSource, if I am using "using" statement in my code, then DataReader will be disposed, its giving me error message.

    I posted that problem here:  http://entlib.codeplex.com/Thread/View.aspx?ThreadId=245208
    Wednesday, February 9, 2011 3:54 PM

Answers

  • User712082397 posted

    You must close the reader immediately after using it. Or, you can close/dispose it off in the "finally" section of the try-catch-finally block. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 15, 2011 3:17 PM

All replies

  • User1831358492 posted

    You will have to close the reader yourself:

    You can close this as RefCountingDataReader.Close;


    Wednesday, February 9, 2011 5:29 PM
  • User712082397 posted

    You must close the reader immediately after using it. Or, you can close/dispose it off in the "finally" section of the try-catch-finally block. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 15, 2011 3:17 PM
  • User619554136 posted

    using will not do any magic regarding connections. It just calls Dispose method at the end of using scope.

    Please read this thread,
    http://forums.asp.net/t/1651995.aspx/1?can+object+exist+after+Dispose+.
    it helps you to understand "Using" and "Dispose".

    So in this case, you would need to call Dispose methdo yourself.

            public void YourMainFunction()
            {
                SqlDataReader reader = GetHospitals();
                reader.Dispose();
            }

    Saturday, February 26, 2011 5:12 PM