共用方式為


SqlCeDataReader.Read 方法

SqlCeDataReader 向前移到下一個資料錄。

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Overrides Function Read As Boolean
'用途
Dim instance As SqlCeDataReader
Dim returnValue As Boolean

returnValue = instance.Read()
public override bool Read()
public:
virtual bool Read() override
abstract Read : unit -> bool 
override Read : unit -> bool 
public override function Read() : boolean

傳回值

型別:System.Boolean
如果有多個資料列則為 true,否則為 false。

實作

IDataReader.Read()

備註

SqlCeDataReader 的預設位置是在第一個資料錄之前。若要開始存取任何資料,則必須呼叫 Read。SqlCeResultSet 的預設位置是第一個資料錄。在建立 SqlCeResultSet 之後立即呼叫 SqlCeResultSet.Read 方法會將資料指標移至第二個資料錄。若為 SqlCeDataReader.Read,其行為便與 SqlCeResultSet.Read 的行為不同。

SqlCeDataReader 在使用中時,相關聯的 SqlCeConnection 會忙於為它提供服務,直到您呼叫 Close 為止。

範例

下列範例會建立 SqlCeConnectionSqlCeCommandSqlCeDataReader。此範例會讀取全部的資料,並將資料寫出至主控台。最後,此範例將關閉 SqlCeDataReader,然後再關閉 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();
}

請參閱

參考

SqlCeDataReader 類別

System.Data.SqlServerCe 命名空間