Does SqlDataAdapter.Fill() always close the connection?

Discussion in 'ASP .Net' started by M, Apr 5, 2005.

  1. M

    M Guest

    Hi,

    Does SqlDataAdapter always close the connection (assuming connection was
    closed before calling Fill()), even if an exception occurs while calling
    Fill()?

    Example:
    try
    {
    myDataAdapter.Fill(myDataTable);
    }
    catch
    {
    // display some error message or something
    }

    or should I do this instead

    try
    {
    myDataAdapter.Fill(myDataTable);
    }
    catch
    {
    // display some error message or something
    }
    finally
    {
    if (conn.State == ConnectionState.Open)
    {
    conn.Close();
    }
    }

    Thanks.
     
    M, Apr 5, 2005
    #1
    1. Advertising

  2. M

    BillGatesFan Guest

    Yes You should close it.
     
    BillGatesFan, Apr 5, 2005
    #2
    1. Advertising

  3. M

    Brock Allen Guest

    SqlDataAdapter manages the connection itself, meaning it opens the connection
    and it closes the connection. If, OTOH, you tell it the connection and you
    open the connection yourself, it detects this and it will not close the connection.
    So since you opened it you're responsible for closing it.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi,
    >
    > Does SqlDataAdapter always close the connection (assuming connection
    > was closed before calling Fill()), even if an exception occurs while
    > calling Fill()?
    >
    > Example:
    > try
    > {
    > myDataAdapter.Fill(myDataTable);
    > }
    > catch
    > {
    > // display some error message or something
    > }
    > or should I do this instead
    >
    > try
    > {
    > myDataAdapter.Fill(myDataTable);
    > }
    > catch
    > {
    > // display some error message or something
    > }
    > finally
    > {
    > if (conn.State == ConnectionState.Open)
    > {
    > conn.Close();
    > }
    > }
    > Thanks.
    >
     
    Brock Allen, Apr 5, 2005
    #3
  4. M

    TDAVISJR Guest

    The dataAdapter manages the connection so you don't have to close it. The
    Adapter does all the work

    --
    TDAVISJR
    aka - Tampa.NET Koder


    "M" <> wrote in message
    news:...
    > Hi,
    >
    > Does SqlDataAdapter always close the connection (assuming connection was
    > closed before calling Fill()), even if an exception occurs while calling
    > Fill()?
    >
    > Example:
    > try
    > {
    > myDataAdapter.Fill(myDataTable);
    > }
    > catch
    > {
    > // display some error message or something
    > }
    >
    > or should I do this instead
    >
    > try
    > {
    > myDataAdapter.Fill(myDataTable);
    > }
    > catch
    > {
    > // display some error message or something
    > }
    > finally
    > {
    > if (conn.State == ConnectionState.Open)
    > {
    > conn.Close();
    > }
    > }
    >
    > Thanks.
    >
    >
     
    TDAVISJR, Apr 5, 2005
    #4
  5. M

    M Guest

    Hi Brock,

    In my example, I did not open the connection. I let SqlDataAdapter open the
    connection. What I'm not sure though, is what happens if .Fill() generates
    an exception. Will the adapter still close the connection or not? Maybe Fill
    has its own try/catch/finally block and therefore will close the connection
    in its own finally even if an error occurred; but that is what I'm not sure
    about.

    Thanks.


    "Brock Allen" <> wrote in message
    news:...
    > SqlDataAdapter manages the connection itself, meaning it opens the
    > connection and it closes the connection. If, OTOH, you tell it the
    > connection and you open the connection yourself, it detects this and it
    > will not close the connection. So since you opened it you're responsible
    > for closing it.
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    >> Hi,
    >>
    >> Does SqlDataAdapter always close the connection (assuming connection
    >> was closed before calling Fill()), even if an exception occurs while
    >> calling Fill()?
    >>
    >> Example:
    >> try
    >> {
    >> myDataAdapter.Fill(myDataTable);
    >> }
    >> catch
    >> {
    >> // display some error message or something
    >> }
    >> or should I do this instead
    >>
    >> try
    >> {
    >> myDataAdapter.Fill(myDataTable);
    >> }
    >> catch
    >> {
    >> // display some error message or something
    >> }
    >> finally
    >> {
    >> if (conn.State == ConnectionState.Open)
    >> {
    >> conn.Close();
    >> }
    >> }
    >> Thanks.
    >>

    >
    >
    >
     
    M, Apr 5, 2005
    #5
  6. M

    M Guest

    Even if Fill causes an exception? Thanks.


    "TDAVISJR" <> wrote in message
    news:%...
    > The dataAdapter manages the connection so you don't have to close it. The
    > Adapter does all the work
    >
    > --
    > TDAVISJR
    > aka - Tampa.NET Koder
    >
    >
    > "M" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> Does SqlDataAdapter always close the connection (assuming connection was
    >> closed before calling Fill()), even if an exception occurs while calling
    >> Fill()?
    >>
    >> Example:
    >> try
    >> {
    >> myDataAdapter.Fill(myDataTable);
    >> }
    >> catch
    >> {
    >> // display some error message or something
    >> }
    >>
    >> or should I do this instead
    >>
    >> try
    >> {
    >> myDataAdapter.Fill(myDataTable);
    >> }
    >> catch
    >> {
    >> // display some error message or something
    >> }
    >> finally
    >> {
    >> if (conn.State == ConnectionState.Open)
    >> {
    >> conn.Close();
    >> }
    >> }
    >>
    >> Thanks.
    >>
    >>

    >
    >
     
    M, Apr 5, 2005
    #6
  7. M

    Brock Allen Guest

    Here's the code in the DataAdapter. QuietOpen and QuietClose open and close
    the connection as long as it wasn't already open when you call Fill:

    try
    {
    try
    {
    DbDataAdapter.QuietOpen(connection1, out state1);
    using (IDataReader reader1 = command.ExecuteReader(behavior
    | CommandBehavior.SequentialAccess))
    {
    if (data is DataTable)
    {
    return this.Fill((DataTable) data, reader1);
    }
    return this.Fill((DataSet) data, srcTable, reader1,
    startRecord, maxRecords);
    }
    }
    finally
    {
    DbDataAdapter.QuietClose(connection1, state1);
    }
    }
    catch
    {
    throw;
    }

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi Brock,
    >
    > In my example, I did not open the connection. I let SqlDataAdapter
    > open the connection. What I'm not sure though, is what happens if
    > .Fill() generates an exception. Will the adapter still close the
    > connection or not? Maybe Fill has its own try/catch/finally block and
    > therefore will close the connection in its own finally even if an
    > error occurred; but that is what I'm not sure about.
    >
    > Thanks.
    >
    > "Brock Allen" <> wrote in message
    > news:...
    >
    >> SqlDataAdapter manages the connection itself, meaning it opens the
    >> connection and it closes the connection. If, OTOH, you tell it the
    >> connection and you open the connection yourself, it detects this and
    >> it will not close the connection. So since you opened it you're
    >> responsible for closing it.
    >>
    >> -Brock
    >> DevelopMentor
    >> http://staff.develop.com/ballen
    >>> Hi,
    >>>
    >>> Does SqlDataAdapter always close the connection (assuming connection
    >>> was closed before calling Fill()), even if an exception occurs while
    >>> calling Fill()?
    >>>
    >>> Example:
    >>> try
    >>> {
    >>> myDataAdapter.Fill(myDataTable);
    >>> }
    >>> catch
    >>> {
    >>> // display some error message or something
    >>> }
    >>> or should I do this instead
    >>> try
    >>> {
    >>> myDataAdapter.Fill(myDataTable);
    >>> }
    >>> catch
    >>> {
    >>> // display some error message or something
    >>> }
    >>> finally
    >>> {
    >>> if (conn.State == ConnectionState.Open)
    >>> {
    >>> conn.Close();
    >>> }
    >>> }
    >>> Thanks.
     
    Brock Allen, Apr 5, 2005
    #7
  8. M

    winladen

    Joined:
    Feb 7, 2009
    Messages:
    1
    I copied this from MSDN. Hope this help

    The Fill method retrieves the data from the data source using a SELECT statement. The IDbConnection object associated with the select command must be valid, but it does not need to be open. If the IDbConnection is closed before Fill is called, it is opened to retrieve data and then closed. If the connection is open before Fill is called, it remains open.

    from : msdn.microsoft.com/en-us/library/zxkb3c3d.aspx
     
    winladen, Feb 7, 2009
    #8
    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. Dan

    TimeOut on SqlDataAdapter.Fill

    Dan, Apr 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    7,843
  2. Paul van Rossem
    Replies:
    0
    Views:
    607
    Paul van Rossem
    Apr 7, 2005
  3. S_K
    Replies:
    6
    Views:
    679
    Russell
    Dec 7, 2007
  4. Andy G

    Using sqlDataAdapter to fill Dataset

    Andy G, Jan 31, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    228
    Elton Wang
    Jan 31, 2005
  5. hyejin
    Replies:
    8
    Views:
    346
    Martin Honnen
    Aug 29, 2006
Loading...

Share This Page