473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Does SqlDataAdapter.Fill() always close the connection?

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
6 10940
Yes You should close it.

Nov 19 '05 #2
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Seiche V via DotNetMonster.com | last post by:
hy. i want to fill a data table from a dataset using SqlDataAdapter.fill(myTable), but i get the error: System.NullReferenceException: Object reference not set to an instance of an object. ...
2
by: Dan | last post by:
I've created a web form which fills a DataGrid with a DataSet generated from the SqlDataAdapter.Fill method. The adapter's query takes about 30 seconds to complete when I run it in the SQL Server...
3
by: alex | last post by:
Hi, I placed a listbox on the left hand side, of an mdi parent, splitter next to it. Docking of the splitter is Left. At the bottom I have a status bar. The list box does not fill the entire...
13
by: david ullua | last post by:
Hi, In Expand.c of BSD system, I met the following codes, the expression (column & 07) if used to compare variable column and 7, if column<=7, it returns true, else false. It use (column & 07)...
1
by: Papa.Coen | last post by:
Hi, I have a problem with my website (aspx/c#); I get a 'Server Application Unavailable' when the host (127.0.0.1/localhost) does not have an network connection (tcp cable unplugged, on purpose ) ...
2
by: Adrien Reboisson | last post by:
I'm trying to build a basic DB explorer using C# & Visual Studio 2005. I installed SQL Server 2005 Express, created a blank project, dropped a TreeView, a ListView and a DataGridView : DB objects...
2
by: active | last post by:
I have another post asking how to fill in the structure but I now think GetDIBBits is the way, I just can't get it to work! I can make the following run but GetDIBBits does not fill the array to...
2
by: Sophy | last post by:
Hi, I do not know why my IE always close when I open the sites that use Prototype framework? Any helps is always appreciate.
0
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.