Export (0) Print
Expand All

SqlDataReader.Close Method

Closes the SqlDataReader object.

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

Implements

IDataReader.Close

Remarks

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.

Example

[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)
    myConnection.Open()
    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.
    myReader.Close()
    ' Close the connection when done with it.
    myConnection.Close()
End Sub 'ReadMyData

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

[C++] 
public:
void ReadMyData(String* myConnString) {
    String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
    SqlConnection* myConnection = new SqlConnection(myConnString);
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
    myConnection->Open();
    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.
    myReader->Close();
    // Close the connection when done with it.
    myConnection->Close();
 }

[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.

Requirements

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

Show:
© 2014 Microsoft