SqlCeDataReader 類別
提供自資料來源讀取資料列之順向 (Forward-Only) 資料流的方法。這個類別無法被繼承。
命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)
語法
'宣告
Public Class SqlCeDataReader _
Inherits DbDataReader
'用途
Dim instance As SqlCeDataReader
public class SqlCeDataReader : DbDataReader
public ref class SqlCeDataReader : public DbDataReader
type SqlCeDataReader =
class
inherit DbDataReader
end
public class SqlCeDataReader extends DbDataReader
備註
若要建立 SqlCeDataReader,則必須呼叫 SqlCeCommand 物件的 ExecuteReader 方法,而不是直接使用建構函式 (Constructor)。
當 SqlCeDataReader 在使用中時,相關聯的 SqlCeConnection 會忙於服務 SqlCeDataReader。在這個狀態下,您可以在同一個連接中建立多個讀取器 (Reader)。
SqlCeDataReader 的使用者可能看得到另一個處理序或執行緒在讀取資料時對結果集所做的變更;但是,確切的行為需視這些事件發生的時間而定。
在 SqlCeDataReader 關閉後,IsClosed 和 RecordsAffected 是唯一可以呼叫的屬性。雖然您可以在 SqlCeDataReader 存在時隨時存取 RecordsAffected 屬性,但在傳回 RecordsAffected 的值之前請一定要呼叫 Close,以確保傳回正確的值。
範例
下列範例會建立 SqlCeConnection、SqlCeCommand 和 SqlCeDataReader。此範例會讀取資料並將資料寫出到主控台,然後再關閉 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();
}
繼承階層
System. . :: . .Object
System. . :: . .MarshalByRefObject
System.Data.Common. . :: . .DbDataReader
System.Data.SqlServerCe..::..SqlCeDataReader
System.Data.SqlServerCe. . :: . .SqlCeResultSet
執行緒安全性
Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.