Export (0) Print
Expand All

OdbcDataReader.Close Method

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Closes the OdbcDataReader 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 finished using the OdbcDataReader to use the associated OdbcConnection for any other purpose.

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 an OdbcConnection, an OdbcCommand, and an OdbcDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the OdbcDataReader, then the OdbcConnection.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New OleDbConnection(myConnString)
    Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As OleDbDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()
        Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
           + myReader.GetString(1))
    End While
    ' always call Close when done reading.
    myReader.Close()
    ' Close the connection when done with it.
    myConnection.Close()
End Sub

[C#] 
public void ReadMyData(string myConnString) 
{
   string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
   OleDbConnection myConnection = new OleDbConnection(myConnString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
   myConnection.Open();
   OleDbDataReader myReader;
   myReader = myCommand.ExecuteReader();
   // Always call Read before accessing data.
   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";
    OleDbConnection* myConnection = new OleDbConnection(myConnString);
    OleDbCommand* myCommand = new OleDbCommand(mySelectQuery,myConnection);
    myConnection->Open();
    OleDbDataReader* myReader;
    myReader = myCommand->ExecuteReader();
    // Always call Read before accessing data.
    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

See Also

OdbcDataReader Class | OdbcDataReader Members | System.Data.Odbc Namespace

Show:
© 2014 Microsoft