473,386 Members | 1,720 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,386 software developers and data experts.

Bad to use datareader in middle tier?

In my middle tier, I, of course, often return data to the presentation
layer. When needing a forward-only, read-only list, I think of the
SqlDataReader (of course). However, since you can't close the connection
before returning the data, the line at the end of the function is what I've
found to use (i.e. CommandBehavior.CloseConnection). HOWEVER, I've heard
this sometimes doesn't always work properly and I should stick to using
DataTables instead.

Have you heard this? Or, do you think the below is just fine?

************************************************** ***

Public Function UserList(ByVal iclientno As Integer) As SqlDataReader

'Return the list of userid from the user table based on a client number.

Dim dbConnection As New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionStringKamtechLogin"))

Dim spCommand As New SqlCommand("spGetUsers", dbConnection)

spCommand.CommandType = CommandType.StoredProcedure

Dim paramClientNo As New SqlParameter("@ClientNo", SqlDbType.Int)

spCommand.Parameters.Add(paramClientNo)

paramClientNo.Direction = ParameterDirection.Input

paramClientNo.Value = iclientno

dbConnection.Open()

Dim dr As SqlDataReader =
spCommand.ExecuteReader(CommandBehavior.CloseConne ction)

Return dr

End Function

************************************************** ***

Thanks,

Ron


Jun 6 '06 #1
5 1266
See my two blogs:

http://spaces.msn.com/sholliday/ 6/5/2006 , 5/24/2006

My preference is to return
DataSets (typed ususally)

or a collection (or <List> in 2.0) of objects. for that simple reason....
the lack of a guaranteed .Close()

(too risky in my opinion)

...


"Ronald S. Cook" <rc***@westinis.com> wrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...
In my middle tier, I, of course, often return data to the presentation
layer. When needing a forward-only, read-only list, I think of the
SqlDataReader (of course). However, since you can't close the connection
before returning the data, the line at the end of the function is what I've found to use (i.e. CommandBehavior.CloseConnection). HOWEVER, I've heard
this sometimes doesn't always work properly and I should stick to using
DataTables instead.

Have you heard this? Or, do you think the below is just fine?

************************************************** ***

Public Function UserList(ByVal iclientno As Integer) As SqlDataReader

'Return the list of userid from the user table based on a client number.

Dim dbConnection As New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionStringKamtechLogi
n"))
Dim spCommand As New SqlCommand("spGetUsers", dbConnection)

spCommand.CommandType = CommandType.StoredProcedure

Dim paramClientNo As New SqlParameter("@ClientNo", SqlDbType.Int)

spCommand.Parameters.Add(paramClientNo)

paramClientNo.Direction = ParameterDirection.Input

paramClientNo.Value = iclientno

dbConnection.Open()

Dim dr As SqlDataReader =
spCommand.ExecuteReader(CommandBehavior.CloseConne ction)

Return dr

End Function

************************************************** ***

Thanks,

Ron



Jun 6 '06 #2

PS.

I don't think it bad to ~use/consume them in the biz layer.

I think its a bad idea to push them on up to the presentation layer.
I wouldn't say I'd 100% never to do it. You have to evaluate who writes
your presentation code.

If its just you, then ..... maybe.

Its its not just you, then..... I'd err on the side of not passing them up.
...
"Ronald S. Cook" <rc***@westinis.com> wrote in message
news:uw**************@TK2MSFTNGP04.phx.gbl...
In my middle tier, I, of course, often return data to the presentation
layer. When needing a forward-only, read-only list, I think of the
SqlDataReader (of course). However, since you can't close the connection
before returning the data, the line at the end of the function is what I've found to use (i.e. CommandBehavior.CloseConnection). HOWEVER, I've heard
this sometimes doesn't always work properly and I should stick to using
DataTables instead.

Have you heard this? Or, do you think the below is just fine?

************************************************** ***

Public Function UserList(ByVal iclientno As Integer) As SqlDataReader

'Return the list of userid from the user table based on a client number.

Dim dbConnection As New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionStringKamtechLogi
n"))
Dim spCommand As New SqlCommand("spGetUsers", dbConnection)

spCommand.CommandType = CommandType.StoredProcedure

Dim paramClientNo As New SqlParameter("@ClientNo", SqlDbType.Int)

spCommand.Parameters.Add(paramClientNo)

paramClientNo.Direction = ParameterDirection.Input

paramClientNo.Value = iclientno

dbConnection.Open()

Dim dr As SqlDataReader =
spCommand.ExecuteReader(CommandBehavior.CloseConne ction)

Return dr

End Function

************************************************** ***

Thanks,

Ron



Jun 6 '06 #3
I agree with sloan. One of the most common posts we get on our forums (and
also that I see here) is running out of connection pool because of rampant
DataReaders that have never been closed.
If you want, you can convert the DatarReader into a DataTable, return that,
and close the reader and the connection before your data ever gets outside
the method it was created in.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Ronald S. Cook" wrote:
In my middle tier, I, of course, often return data to the presentation
layer. When needing a forward-only, read-only list, I think of the
SqlDataReader (of course). However, since you can't close the connection
before returning the data, the line at the end of the function is what I've
found to use (i.e. CommandBehavior.CloseConnection). HOWEVER, I've heard
this sometimes doesn't always work properly and I should stick to using
DataTables instead.

Have you heard this? Or, do you think the below is just fine?

************************************************** ***

Public Function UserList(ByVal iclientno As Integer) As SqlDataReader

'Return the list of userid from the user table based on a client number.

Dim dbConnection As New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionStringKamtechLogin"))

Dim spCommand As New SqlCommand("spGetUsers", dbConnection)

spCommand.CommandType = CommandType.StoredProcedure

Dim paramClientNo As New SqlParameter("@ClientNo", SqlDbType.Int)

spCommand.Parameters.Add(paramClientNo)

paramClientNo.Direction = ParameterDirection.Input

paramClientNo.Value = iclientno

dbConnection.Open()

Dim dr As SqlDataReader =
spCommand.ExecuteReader(CommandBehavior.CloseConne ction)

Return dr

End Function

************************************************** ***

Thanks,

Ron



Jun 6 '06 #4
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:C1**********************************@microsof t.com...
I agree with sloan. One of the most common posts we get on our forums (and
also that I see here) is running out of connection pool because of rampant
DataReaders that have never been closed.


Sure, but that doesn't necessarily mean that DataReaders in and of
themselves are bad just because some developers don't know how to close them
properly...
Jun 6 '06 #5
Mark,
I didn't say DataReaders were "bad"; I even suggested using a DataReader to
get a DataTable and then closing the connection before returning same. We
all are aware that the "non-disconnected" behavior of the DataReader lends
itself to developer error.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mark Rae" wrote:
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:C1**********************************@microsof t.com...
I agree with sloan. One of the most common posts we get on our forums (and
also that I see here) is running out of connection pool because of rampant
DataReaders that have never been closed.


Sure, but that doesn't necessarily mean that DataReaders in and of
themselves are bad just because some developers don't know how to close them
properly...

Jun 7 '06 #6

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

Similar topics

10
by: Kumar Saurabh | last post by:
Hi Devs, I was thinking of implementing a middle tire (most probably in XML) for a PHP MySQL web app but couldn't make a head way. can any one suggest how can i proceed with this. Thanks in...
1
by: Mr. Smart | last post by:
I have a middle-tier written using Foxpro. I'd like this tier to coommunicate (that is pass some variables to ASP). I defined a Session variable somewhere in my ASP file, e.g. Session("Usercode") =...
62
by: SAN3141 | last post by:
There doesn't seem to be consensus about when to put code in the database or in the middle tier. There was a long discussion about this in an Oracle newsgroup (message ID:...
2
by: billym | last post by:
Does anyone know if C# (or .NET applications in general) can be load balanced on the middle tier simply by deploying them to multiple servers in a clustered environtment? If so, would this be...
0
by: JasonP | last post by:
Hi, I am trying to decide the best middle tier method for the system I am currently building. There are no issues of supporting previous software versions, etc as this is a completely new...
6
by: Ian Williamson | last post by:
Greetings, My company has an ASP.NET based enterprise product that is undergoing some changes and I need some community input to help solve a problem. In the current implementation, any given...
1
by: dgk | last post by:
How can I create a middle tier object using the standard databinding stuff? All the examples that I see in the books using the new 2005 controls just shows how to build stuff in two tiers. It...
3
by: Jules | last post by:
Sorry for being contentious title, but I am confused and concerned over the level of MS infrastructure support to the business logic layer. MS have great support to Web services, for EAI and B2B...
3
by: SevDer | last post by:
Hello All, For my ASP.NET application, I am trying to have my middle tier (currently logical layer) dll's to be loaded dynamically. The aim is not to kill the sessions when I need to update...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.