473,407 Members | 2,359 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,407 software developers and data experts.

Close question

I have some code which I will show below that opens a SQLDataReader, gets
some information and then closes. My question is, what is the proper way to
close? Do I close the reader first and then attempt to close SQLCommand and
the SQLConnection? Do I close the SQLCommand and the SQLConnection and then
the SQLDataReader? Do I need to check the conneciton state for each?

What is the proper way?

mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select * from VSPW", _
mySqlConnection)

Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
psMasterPW = myReader.GetString(0) ' The first field in the
answer set
psEditARPW = myReader.GetString(1) 'The second field in the
answer set
psPrintPW = myReader.GetString(2) 'The third field in the
answer set
End While
'myReader.Close()
'sql_Command.Connection.Close()
'If mySqlConnection.State = ConnectionState.Open Then
' mySqlConnection.Close()
'End If

Catch ex As SqlException
MessageBox.Show(ex.ToString, "Exception Error")
Finally
If sql_Command.Connection.State = ConnectionState.Open Then
sql_Command.Connection.Close()
End If
If mySqlConnection.State = ConnectionState.Open Then
mySqlConnection.Close()
End If
sql_Command.Connection.Close()
End Try
Nov 20 '05 #1
3 1514
Fire your reader specifying the CommandBehavior.CloseConnection enum
http://www.knowdotnet.com/articles/schemas2.html
And then just close your connection afterward. The only thing you'll
actually close is the connection and datareader not the command.

HTH,

Bill
"Woody Splawn" <wo***@splawns.com> wrote in message
news:uU**************@tk2msftngp13.phx.gbl...
I have some code which I will show below that opens a SQLDataReader, gets
some information and then closes. My question is, what is the proper way to close? Do I close the reader first and then attempt to close SQLCommand and the SQLConnection? Do I close the SQLCommand and the SQLConnection and then the SQLDataReader? Do I need to check the conneciton state for each?

What is the proper way?

mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select * from VSPW", _
mySqlConnection)

Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
psMasterPW = myReader.GetString(0) ' The first field in the answer set
psEditARPW = myReader.GetString(1) 'The second field in the answer set
psPrintPW = myReader.GetString(2) 'The third field in the
answer set
End While
'myReader.Close()
'sql_Command.Connection.Close()
'If mySqlConnection.State = ConnectionState.Open Then
' mySqlConnection.Close()
'End If

Catch ex As SqlException
MessageBox.Show(ex.ToString, "Exception Error")
Finally
If sql_Command.Connection.State = ConnectionState.Open Then
sql_Command.Connection.Close()
End If
If mySqlConnection.State = ConnectionState.Open Then
mySqlConnection.Close()
End If
sql_Command.Connection.Close()
End Try

Nov 20 '05 #2
Hi Woody

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to know close the
datareader and its according resource.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I agree with Bill's suggestion, you do not need to close the SQLCommand.
Its Connection is the same one the sqlcommand are using.

SqlCommand.Connection Property .
Gets or sets the SqlConnection used by this instance of the SqlCommand.
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdatasqlclientsqlcommandclassconnectiont opic.asp

Retrieving Data Using the DataReader
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpcontheadonetdatareader.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #3
Hi Woody

Thanks for posting in the community.

Did my suggestion help you?
If you have any concern on this issue, please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #4

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

Similar topics

2
by: InvisibleMan | last post by:
Hi, I feel a little dumb for asking this (considering im writing TSQL) but there doesn't seem to be any definitive answers on the search engines... Okay I understand that if you open the ADO...
3
by: Martin | last post by:
Hi, this is probably a very basic question but I'm not a javascript expert! Sure you get this asked often! Did some research but can't get it working. I'm thankful for any help! Thanks! ...
4
by: Raymond Wilk | last post by:
I used javascript to open a new window, then used the following to close the new window on the next click: <body onBlur="self.close()" onClick="self.close()" bgcolor="#FFFFFF"> On the new window...
8
by: Paul W | last post by:
Hi - in an asp.net application, how should I close out a sqlclient.connection? What combination and order of Conn.close conn.dispose conn=nothing Should I use? Specifically, is the...
2
by: mark | last post by:
I put this question on a new thread because, in spirit, it is a new question. I apologize that I have difficulty formulating the proper question. My application performs matrix computations on...
7
by: MichaelK | last post by:
What's the best way to close current window silently after finished a process. I'm using the the javascritpt window.close() or top.window.close(), but it fires a confirmation window. Just want to...
7
by: tapanreddy | last post by:
I am looking to perform an action when we close the window using the close tab at the top of the screen. I know how to do it using a close button but I was wondering if there is way to achieve the...
15
by: Mahernoz | last post by:
Hi Friends, I simply want to print a report by opening a popup, calling the print function and closing the window after the user has printed the report. I have 2 functions.... in c# The...
5
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I am showing a form by ShowModal(). I put a button on that form. When a user clicks the button a MsgBox will show with question "do you want to close?" yes/no. How to handle the situation: - user...
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
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: 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:
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
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.