DataReader and DB Connection Closing

Discussion in 'ASP .Net' started by rockdale, Sep 21, 2006.

  1. rockdale

    rockdale Guest

    I am using Microsoft.Practices.Enterprise.Library

    When I use IDataReader I saw this line
    "It is the responsibility of the caller to close the connection and
    reader when finished."

    I was wondering if I close the DataReader do I automatic ally close the
    Connection? If not , how can I close the Connection outside the
    ExecuteReader function?

    I code snippet is like following:

    ------------------------------------------------
    IDataReader oDr =null;

    Database db = DatabaseFactory.CreateDatabase();

    string sqlCommand = "my_stored_proc";
    DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);


    oDr = db.ExecuteReader(dbCommand);


    /*DO SOMETHING WITH oDR*/


    /*I close the DataReader here, do I need to close connection also?*/
    oDr.Close();

    -------------------------------------------------
    Thanks

    -rockdale
    rockdale, Sep 21, 2006
    #1
    1. Advertising

  2. Rockdale,

    Here is some info on closing datareaders.

    I always wrap my code in a try / finally block and put the close into the
    finally so that even if an error is encountered the reader and connection
    are still closed.

    http://msdn2.microsoft.com/en-us/library/haa3afyz.aspx

    Regards,

    --
    S. Justin Gengo
    Web Developer / Programmer

    Free code library:
    http://www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche


    "rockdale" <> wrote in message
    news:...
    >I am using Microsoft.Practices.Enterprise.Library
    >
    > When I use IDataReader I saw this line
    > "It is the responsibility of the caller to close the connection and
    > reader when finished."
    >
    > I was wondering if I close the DataReader do I automatic ally close the
    > Connection? If not , how can I close the Connection outside the
    > ExecuteReader function?
    >
    > I code snippet is like following:
    >
    > ------------------------------------------------
    > IDataReader oDr =null;
    >
    > Database db = DatabaseFactory.CreateDatabase();
    >
    > string sqlCommand = "my_stored_proc";
    > DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    >
    >
    > oDr = db.ExecuteReader(dbCommand);
    >
    >
    > /*DO SOMETHING WITH oDR*/
    >
    >
    > /*I close the DataReader here, do I need to close connection also?*/
    > oDr.Close();
    >
    > -------------------------------------------------
    > Thanks
    >
    > -rockdale
    >
    S. Justin Gengo, Sep 21, 2006
    #2
    1. Advertising

  3. Yes, I would also recommend explicit disposing connection objects. It's a
    got idea to put it in a finally block.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "rockdale" <> wrote in message
    news:...
    >I am using Microsoft.Practices.Enterprise.Library
    >
    > When I use IDataReader I saw this line
    > "It is the responsibility of the caller to close the connection and
    > reader when finished."
    >
    > I was wondering if I close the DataReader do I automatic ally close the
    > Connection? If not , how can I close the Connection outside the
    > ExecuteReader function?
    >
    > I code snippet is like following:
    >
    > ------------------------------------------------
    > IDataReader oDr =null;
    >
    > Database db = DatabaseFactory.CreateDatabase();
    >
    > string sqlCommand = "my_stored_proc";
    > DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    >
    >
    > oDr = db.ExecuteReader(dbCommand);
    >
    >
    > /*DO SOMETHING WITH oDR*/
    >
    >
    > /*I close the DataReader here, do I need to close connection also?*/
    > oDr.Close();
    >
    > -------------------------------------------------
    > Thanks
    >
    > -rockdale
    >
    Eliyahu Goldin, Sep 21, 2006
    #3
  4. rockdale

    rockdale Guest

    Hi, Eliyahu

    Thanks for the reply.
    But how can I dispose the connection object as the db connection is
    created and opened in the database factory

    I know I must close the connection if I create and open this connection
    but here as I am using the Data Access block, how do I explicitly close
    the connection and dispose it? Or should I use dataset since "The
    connection is closed by the ExecuteDataSet function" ? (comment from
    Microsoft Enterprise Library)

    Thanks again
    -phelix

    Eliyahu Goldin wrote:
    > Yes, I would also recommend explicit disposing connection objects. It's a
    > got idea to put it in a finally block.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]
    >
    >
    > "rockdale" <> wrote in message
    > news:...
    > >I am using Microsoft.Practices.Enterprise.Library
    > >
    > > When I use IDataReader I saw this line
    > > "It is the responsibility of the caller to close the connection and
    > > reader when finished."
    > >
    > > I was wondering if I close the DataReader do I automatic ally close the
    > > Connection? If not , how can I close the Connection outside the
    > > ExecuteReader function?
    > >
    > > I code snippet is like following:
    > >
    > > ------------------------------------------------
    > > IDataReader oDr =null;
    > >
    > > Database db = DatabaseFactory.CreateDatabase();
    > >
    > > string sqlCommand = "my_stored_proc";
    > > DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
    > >
    > >
    > > oDr = db.ExecuteReader(dbCommand);
    > >
    > >
    > > /*DO SOMETHING WITH oDR*/
    > >
    > >
    > > /*I close the DataReader here, do I need to close connection also?*/
    > > oDr.Close();
    > >
    > > -------------------------------------------------
    > > Thanks
    > >
    > > -rockdale
    > >
    rockdale, Sep 21, 2006
    #4
  5. rockdale

    deepakkarma

    Joined:
    Jun 27, 2011
    Messages:
    1
    public IDataReader DsFetch()
    {
    IDataReader Idr = null;
    try
    {

    Database db = DatabaseFactory.CreateDatabase();
    DbCommand dbcommand = db.GetSqlStringCommand("select * from RSSrnd");
    dbcommand.CommandType = CommandType.Text;
    return Idr =db.ExecuteReader(dbcommand);

    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {

    }
    }
    deepakkarma, Jun 27, 2011
    #5
  6. rockdale

    Rafael Neris

    Joined:
    Jan 31, 2012
    Messages:
    1
    Hi Rockdale,

    In fact the ExecuteReader method of the Database class, automatically closes its connection to the DB after the closing of the DataReader returned by the method.

    This is because it uses CommandBehavior.CloseConnection.

    Note the class source Database.cs Microsoft Practices.

    I hope this helps.

    Undo edits


    Source Database.cs:



    public virtual IDataReader ExecuteReader(DbCommand command)
    {
    DbConnection connection = OpenConnection();
    PrepareCommand(command, connection);


    try
    {


    return DoExecuteReader(command, CommandBehavior.CloseConnection);
    }


    catch
    {
    connection.Close();


    throw;
    }
    }
    Rafael Neris, Jan 31, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paolo Pignatelli

    Closing DataReader using ApplicationBlocks

    Paolo Pignatelli, Feb 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,567
    Joe Fallon
    Feb 26, 2005
  2. Arsalan
    Replies:
    7
    Views:
    742
    Rob Nicholson
    Apr 2, 2005
  3. johnny
    Replies:
    1
    Views:
    776
    Jerry Hill
    Dec 8, 2006
  4. Closing a datareader

    , Jun 9, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    667
    Mark Rae
    Jun 9, 2007
  5. shmuel witman

    Connection timeout (connection closing after 100 sec )

    shmuel witman, Dec 4, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    773
    shmuel witman
    Dec 6, 2005
Loading...

Share This Page