Méthode SqlCeDataReader.Seek

Place SqlCeDataReader sur l'enregistrement ayant des valeurs indexées qui correspondent aux paramètres spécifiés.

Espace de noms :  System.Data.SqlServerCe
Assembly :  System.Data.SqlServerCe (en System.Data.SqlServerCe.dll)

Syntaxe

'Déclaration
<SecurityCriticalAttribute> _
<SecurityTreatAsSafeAttribute> _
<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode := True)> _
Public Function Seek ( _
    dbSeekOptions As DbSeekOptions, _
    ParamArray index As Object() _
) As Boolean
'Utilisation
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

Paramètres

Valeur de retour

Type : System.Boolean
Valeur booléenne ; true indique que le curseur est positionné sur une ligne.

Exceptions

Exception Condition
SqlCeException

La valeur est introuvable ou autre erreur s'est produite.

Notes

Cette méthode est destinée à représenter une solution de remplacement plus rapide que l'instruction SELECT pour la récupération d'une ligne à partir d'une table de base. À la place d'une clause WHERE dans une instruction SELECT, vous pouvez utiliser Seek pour récupérer rapidement une ligne en fonction de sa valeur d'index. Par exemple, pour récupérer un employé ayant un ID d'employé compris entre 1 et 5, vous pouvez exécuter une instruction SELECT, mais l'utilisation de Seek ayant une valeur 5 sur l'index d'ID de l'employé améliorera considérablement les performances.

Seek peut être utilisé uniquement quand CommandType a pour valeur TableDirect, CommandText a pour valeur un nom de table de base valide et IndexName a pour valeur un nom d'index valide sur la table de base spécifiée.

Après l'utilisation de Seek, SqlCeDataReader retourne les lignes dans leur ordre d'index. Lorsque Seek est utilisé sur un SqlCeDataReader possédant une plage spécifiée par SetRange, Seek se positionne uniquement sur les lignes se situant dans cette plage. Pour plus d'informations, consultez « IRowsetIndex::Seek » dans la documentation OLE DB.

Exemples

        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);
        }

Voir aussi

Référence

SqlCeDataReader Classe

Espace de noms System.Data.SqlServerCe