By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,000 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,000 IT Pros & Developers. It's quick & easy.

Does SqlDataAdapter.Fill() always close the connection?

P: n/a
M
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.
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Yes You should close it.

Nov 19 '05 #2

P: n/a
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.


Nov 19 '05 #3

P: n/a
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" <so******@somewhere.com> wrote in message
news:Oj**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 19 '05 #4

P: n/a
M
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" <ba****@NOSPAMdevelop.com> wrote in message
news:37**********************@msnews.microsoft.com ...
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.


Nov 19 '05 #5

P: n/a
M
Even if Fill causes an exception? Thanks.
"TDAVISJR" <an*******@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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" <so******@somewhere.com> wrote in message
news:Oj**************@TK2MSFTNGP14.phx.gbl...
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.


Nov 19 '05 #6

P: n/a
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" <ba****@NOSPAMdevelop.com> wrote in message
news:37**********************@msnews.microsoft.com ...
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.


Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.