SqlCeDataReader.Seek 方法

SqlCeDataReader 放置在包含与指定参数匹配的索引值的记录上。

命名空间:  System.Data.SqlServerCe
程序集:  System.Data.SqlServerCe(在 System.Data.SqlServerCe.dll 中)

语法

声明
<SecurityCriticalAttribute> _
<SecurityTreatAsSafeAttribute> _
<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode := True)> _
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)
[SecurityCriticalAttribute]
[SecurityTreatAsSafeAttribute]
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public bool Seek(
    DbSeekOptions dbSeekOptions,
    params Object[] index
)
[SecurityCriticalAttribute]
[SecurityTreatAsSafeAttribute]
[SecurityPermissionAttribute(SecurityAction::Assert, UnmanagedCode = true)]
public:
bool Seek(
    DbSeekOptions dbSeekOptions, 
    ... array<Object^>^ index
)
[<SecurityCriticalAttribute>]
[<SecurityTreatAsSafeAttribute>]
[<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)>]
member Seek : 
        dbSeekOptions:DbSeekOptions * 
        index:Object[] -> bool 
public function Seek(
    dbSeekOptions : DbSeekOptions, 
    ... index : Object[]
) : boolean

参数

返回值

类型:System.Boolean
一个布尔值;如果是 true,则表示将光标定位于某行。

异常

异常 条件
SqlCeException

没有找到该值,或发生了其他错误。

注释

此方法可用于代替 SELECT 语句,以更快的速度从基表中检索行。可以不在 SELECT 语句中使用 WHERE 子句,而使用 Seek 来基于行的索引值迅速检索行集。例如,要检索雇员 ID 为 5 的员工,您可以使用 SELECT 语句,但是根据 5 这一雇员 ID 索引值来使用 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 类

System.Data.SqlServerCe 命名空间