This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708
You are currently viewing the BOOK: Beginning ASP.NET 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
based on the the examples in the book, i wrote the following code.
This code return a sqldatareader to a record set. My question is
how does one close the connection? in order words, what does the
following statment actually do?
Dim dr As SqlDataReader =
spCommand.ExecuteReader(CommandBehavior.CloseConne ction)
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)
The CommandBehavior.CloseConnection enum makes sure that the Connection is closed when the associated DataReader is closed.
Consider this:
MySQLDataReader = UserList(10)
' Do something useful with MySQLDataReader
MySQLDataReader.Close()
As soon as you close the reader, the connection it was using is then closed automatically as well. Because Readers are connected objects (the need an open connection to function correctly), you can't close the Connection before you are done with the reader. By specifying the CommandBehavior.CloseConnection enum, the creator of the UserList method can make sure that the connection is closed as early as possible.
So, it's important to explicitly close your reader, or you'll end up with open connections.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Hi, I have an issue with following code. When this code is executed it shows open connection in my database of Sql Server. I run the 'EXEC SP_WHO' in sql query analyzer. How to actually close reader and connection? Please help me.....