共用方式為


Seek 方法

SqlCeDataReader 放到具有符合指定參數之索引值的資料錄上。

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

語法

'宣告
Public Function Seek ( _
    dbSeekOptions As DbSeekOptions, _
    ParamArray index As Object() _
) As Boolean
'用途
Dim instance As SqlCeDataReader
Dim dbSeekOptions As DbSeekOptions
Dim index As Object()
Dim returnValue As Boolean

returnValue = instance.Seek(dbSeekOptions, _
    index)
public bool Seek(
    DbSeekOptions dbSeekOptions,
    params Object[] index
)
public:
bool Seek(
    DbSeekOptions dbSeekOptions, 
    ... array<Object^>^ index
)
member Seek : 
        dbSeekOptions:DbSeekOptions * 
        index:Object[] -> bool 
public function Seek(
    dbSeekOptions : DbSeekOptions, 
    ... index : Object[]
) : boolean

參數

傳回值

型別:System. . :: . .Boolean
布林值,true 指示游標已在資料列上。

例外

例外狀況 條件
SqlCeException

找不到值,或發生其他錯誤。

備註

這個方法通常用於做為 SELECT 陳述式的替代方法,以便使用較快的速度從基底資料表擷取資料列集。與其使用 SELECT 陳述式中的 WHERE 子句,Seek 可以用來根據資料列的索引值而快速擷取資料列。例如,若要擷取員工識別碼為 5 的員工,可以執行 SELECT 陳述式,但是在員工識別碼索引上使用值為 5 的 Seek 將可大幅增進效能。

只有在 CommandType 設定為 TableDirect、CommandText 設定為有效的基底資料表名稱,而 IndexName 設定為所指定基底資料表上的有效索引名稱時,才能使用 Seek。

在使用 Seek 之後,SqlCeDataReader 會依照資料列的索引順序,傳回剩餘的資料列。在具有 SetRange 所指定範圍的 SqlCeDataReader 上使用 Seek 時,Seek 只能放在範圍內的資料列上。如需詳細資訊,請參閱 OLE DB 文件中的<IRowsetIndex::Seek>主題。

範例

        Try
            Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")
            conn.Open()

            Dim cmd As SqlCeCommand = conn.CreateCommand()
            cmd.CommandType = CommandType.TableDirect
            cmd.IndexName = "Orders_PK"
            cmd.CommandText = "Orders"

            ' We are interested in orders that match Order ID = 10020
            '
            cmd.SetRange(DbRangeOptions.Match, New Object() {10020}, Nothing)

            Dim reader As SqlCeDataReader = cmd.ExecuteReader(CommandBehavior.Default)

            While reader.Read()
                MessageBox.Show(String.Format("{0} ; {1}", reader("Order ID"), reader("Order Date")))
            End While

            ' Now we are interested in orders with Order ID between (10020, 10050)
            '
            cmd.SetRange(DbRangeOptions.InclusiveStart Or DbRangeOptions.InclusiveEnd, New Object() {10020}, New Object() {10050})

            reader = cmd.ExecuteReader(CommandBehavior.Default)

            ' Now seek to Order ID = 10045
            '
            Dim onRow As Boolean =  reader.Seek(DbSeekOptions.FirstEqual, New Object() {10045})

            ' Now ,the reader will return rows with Order ID >= 10045 <= 10050
            ' because the range was set to (10020, 10050)
            '
            If onRow Then
                While reader.Read()
                    MessageBox.Show(String.Format("{0} ; {1}", reader("Order ID"), reader("Order Date")))
                End While
            End If
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try
        try
        {
            SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");
            conn.Open();

            SqlCeCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.TableDirect;
            cmd.IndexName = "Orders_PK";
            cmd.CommandText = "Orders";

            // We are interested in orders that match Order ID = 10020
            //
            cmd.SetRange(DbRangeOptions.Match, new object[] { 10020 }, null);

            SqlCeDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);

            for (int i = 1; reader.Read(); i++)
            {
                MessageBox.Show(String.Format("{0} ; {1}", reader["Order ID"], reader["Order Date"]));
            }

            // Now we are interested in orders with Order ID between (10020, 10050)
            //
            cmd.SetRange(DbRangeOptions.InclusiveStart | DbRangeOptions.InclusiveEnd,
                new object[] { 10020 }, new object[] { 10050 });

            reader = cmd.ExecuteReader(CommandBehavior.Default);

            // Now seek to Order ID = 10045
            //
            bool onRow = reader.Seek(DbSeekOptions.FirstEqual, new object[] { 10045 });

            // Now ,the reader will return rows with Order ID >= 10045 <= 10050
            // because the range was set to (10020, 10050)
            //
            if (onRow)
            {
                for (int i = 1; reader.Read(); i++)
                {
                    MessageBox.Show(String.Format("{0} ; {1}", reader["Order ID"], reader["Order Date"]));
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

請參閱

參考

SqlCeDataReader類別

SqlCeDataReader 成員

System.Data.SqlServerCe 命名空間