This documentation is archived and is not being maintained.

SqlDataReader.Close Method

Closes the SqlDataReader object.

[Visual Basic]
Public Overridable Sub Close() Implements IDataReader.Close
public virtual void Close();
public: virtual void Close();
public function Close();




You must explicitly call the Close method when you are through using the SqlDataReader to use the associated SqlConnection for any other purpose.

The Close method fills in the values for output parameters, return values and RecordsAffected, increasing the amount of time it takes to close a SqlDataReader that was used to process a large or complicated query. In cases where the return values and the number of records affected by a query are not significant, the amount of time it takes to close the SqlDataReader can be reduced by calling the Cancel method of the associated SqlCommand object before calling the Close method.

CAUTION   Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, you should only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. For more information, see Programming for Garbage Collection.


[Visual Basic, C#, C++] The following example creates a SqlConnection, a SqlCommand, and a SqlDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the SqlDataReader, then the SqlConnection.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New SqlConnection(myConnString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    Dim myReader As SqlDataReader
    myReader = myCommand.ExecuteReader()
    While myReader.Read()
        Console.WriteLine(myReader.GetInt32(0) & ", " & myReader.GetString(1))
    End While
    ' always call Close when done reading.
    ' Close the connection when done with it.
End Sub 'ReadMyData

public void ReadMyData(string myConnString) {
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    SqlDataReader myReader;
    myReader = myCommand.ExecuteReader();
    while (myReader.Read()) 
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    // always call Close when done reading.
    // Close the connection when done with it.

void ReadMyData(String* myConnString) {
    String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
    SqlConnection* myConnection = new SqlConnection(myConnString);
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
    SqlDataReader* myReader;
    myReader = myCommand->ExecuteReader();
    while (myReader->Read()) 
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
    // always call Close when done reading.
    // Close the connection when done with it.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

SqlDataReader Class | SqlDataReader Members | System.Data.SqlClient Namespace