Share via


SqlCeDataReader.Close Method

Chiude l'oggetto SqlCeDataReader.

Spazio dei nomi: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Sintassi

'Dichiarazione
Public Overrides Sub Close
public override void Close ()
public:
virtual void Close () override
public void Close ()
public override function Close ()

Osservazioni

Per utilizzare l'oggetto SqlCeConnection per qualsiasi altro scopo, è necessario innanzitutto chiamare esplicitamente il metodo Close mentre si sta utilizzando l'oggetto SqlCeDataReader; è tuttavia possibile creare più visualizzatori nella stessa connessione.

Esempio

Nell'esempio che segue vengono creati un oggetto SqlCeConnection, un oggetto SqlCeCommand e un oggetto SqlCeDataReader. Nell'esempio i dati vengono letti e poi scritti nella console. Infine, vengono chiusi l'oggetto SqlCeDataReader e quindi l'oggetto SqlCeConnection.

Dim conn As SqlCeConnection = Nothing
Dim cmd As SqlCeCommand = Nothing
Dim rdr As SqlCeDataReader = Nothing

Try
    ' Open the connection and create a SQL command
    '
    conn = New SqlCeConnection("Data Source = AdventureWorks.sdf")
    conn.Open()
    
    cmd = New SqlCeCommand("SELECT * FROM DimEmployee", conn)
    
    rdr = cmd.ExecuteReader()
    
    ' Iterate through the results
    '
    While rdr.Read()
        Dim employeeID As Integer = rdr.GetInt32(0) ' or: rdr["EmployeeKey"];
        Dim lastName As String = rdr.GetString(5) ' or: rdr["FirstName"];
    End While
    
    ' Always dispose data readers and commands as soon as practicable
    '
    rdr.Close()
    cmd.Dispose()
Finally
    ' Close the connection when no longer needed
    '
    conn.Close()
End Try
SqlCeConnection conn = null;
SqlCeCommand cmd = null;
SqlCeDataReader rdr = null;

try
{
    // Open the connection and create a SQL command
    //
    conn = new SqlCeConnection("Data Source = AdventureWorks.sdf");
    conn.Open();

    cmd = new SqlCeCommand("SELECT * FROM DimEmployee", conn);

    rdr = cmd.ExecuteReader();

    // Iterate through the results
    //
    while (rdr.Read())
    {
        int employeeID = rdr.GetInt32(0);   // or: rdr["EmployeeKey"];
        string lastName = rdr.GetString(5); // or: rdr["FirstName"];
    }

    // Always dispose data readers and commands as soon as practicable
    //
    rdr.Close();
    cmd.Dispose();
}
finally
{
    // Close the connection when no longer needed
    //
    conn.Close();
}

Affidabilità

Tutti i membri statici pubblici (Shared in Microsoft Visual Basic) di questo tipo sono affidabili. Non è invece garantita l'affidabilità dei membri dell'istanza.

Piattaforme

Piattaforme di sviluppo

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Informazioni sulla versione
.NET Framework e .NET Compact Framework
Supportato in 3.5
.NET Framework
Supportato in 3.0
.NET Compact Framework e .NET Framework
Supportato in 2.0

Vedere anche

Riferimento

SqlCeDataReader Class
SqlCeDataReader Members
System.Data.SqlServerCe Namespace