473,756 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataReader and DB Connection Closing

I am using Microsoft.Pract ices.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.GetStoredPro cCommand(sqlCom mand);
oDr = db.ExecuteReade r(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

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

-rockdale

Sep 21 '06 #1
3 2785
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" <ro************ @gmail.comwrote in message
news:11******** *************@k 70g2000cwa.goog legroups.com...
>I am using Microsoft.Pract ices.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.GetStoredPro cCommand(sqlCom mand);
oDr = db.ExecuteReade r(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

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

-rockdale

Sep 21 '06 #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" <ro************ @gmail.comwrote in message
news:11******** *************@k 70g2000cwa.goog legroups.com...
>I am using Microsoft.Pract ices.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.GetStoredPro cCommand(sqlCom mand);
oDr = db.ExecuteReade r(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

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

-rockdale

Sep 21 '06 #3
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" <ro************ @gmail.comwrote in message
news:11******** *************@k 70g2000cwa.goog legroups.com...
I am using Microsoft.Pract ices.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.GetStoredPro cCommand(sqlCom mand);
oDr = db.ExecuteReade r(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

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

-rockdale
Sep 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
19534
by: Craig | last post by:
I have some methods that open a database connection, get some data and then return a datareader. How do I manage closing the connection to the database then? public OracleDataReader ExecuteCommand(string cmdStr) { Connect(); OracleCommand cmd = new OracleCommand(cmdStr, this._conn); OracleDataReader reader = cmd.ExecuteReader(); return reader; }
5
2839
by: Joel Reinford | last post by:
I am attempting to verify whether a datareader is closed by calling the databind on a control. for example (fake code) Dim rdr as SqlDataReader rdr = mycommand.executeReader... MyDataGrid.DataSource = rdr MyDataGrid.DataBind()
20
5530
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to "clean up" the DataView once it is not referenced and will not effect the number of connections to the database. A DataReader on the other hand always maintains a connection to the database and must be explicitly closed (Do not rely on garbage...
2
1579
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also there's the place where I declare one public static DataReader, but I'm not sure this is the best way to do it. I use that DataReader in all the webforms and user controls, whenever I have to read data from the db. So, should I do it this way or...
3
1834
by: Paolo Pignatelli | last post by:
I have a Function in a Class File that uses ApplicationBlocks: Public Function GetProductsByCategory(ByVal CategoryID As Integer) Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim ProductsByCategory As SqlDataReader = SqlHelper.ExecuteReader(myConnection, CommandType.StoredProcedure, "ProductsByCategory", New SqlParameter("@CategoryID", CategoryID))
3
1866
by: Lubomir | last post by:
Hi, I have a class A with a public method GetReader: public OleDbDataReader GetReader() { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings); SqlCommand myCommand = new SqlCommand("CMRC_Top10List", myConnection); myCommand.CommandType = CommandType.StoredProcedure;
7
4130
by: Arsalan | last post by:
I have a function which return datareader Public Shared Function ReturnDReader(ByVal query As String) As OleDbDataReader Dim Connection_String As String = System.Configuration.ConfigurationSettings.AppSettings("strConn") Dim conn As OleDbConnection Dim cm As OleDbCommand Dim dr As OleDbDataReader Try
17
2382
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data, this method doesn't close it, it just returns the datareader. The calling code uses the datareader and then just lets it drop out of scope, to be picked up by the garbage collector. Is this a problem? A friend of mine suggested to me that not...
7
2905
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and returns N rows. DataReader object? Collection object? DataTable object? Returning a DataReader object is not a good practice...right? Thnks for all your suggestions!!
20
7214
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I get the error "There is already an open DataReader associated with this Connection which must be closed first." How can I fix this error ? For each DataReader, do I want to open and close the connection (in this case adoCon) to avoid this error ?...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10040
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.