SqlCeDataReader.Seek Method
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 (dans system.data.sqlserverce.dll)
Syntaxe
'Déclaration
Public Function Seek ( _
dbSeekOptions As DbSeekOptions, _
ParamArray index As Object() _
) As Boolean
public bool Seek (
DbSeekOptions dbSeekOptions,
params Object[] index
)
public:
bool Seek (
DbSeekOptions dbSeekOptions,
... array<Object^>^ index
)
public boolean Seek (
DbSeekOptions dbSeekOptions,
Object[] index
)
public function Seek (
dbSeekOptions : DbSeekOptions,
... index : Object[]
) : boolean
Paramètres
- dbSeekOptions
DbSeekOptions à utiliser.
- index
Index de l'enregistrement.
Valeur de retour
Valeur booléenne ; true indique que le curseur est positionné sur une ligne.
Exceptions
Type d'exception | Condition |
---|---|
SqlCeException | La valeur est introuvable ou autre erreur s'est produite. |
Remarques
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.
Exemple
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);
}
Sécurité des threads
Tout membre statique public (Partagé dans Microsoft Visual Basic) de ce type sont thread-safe. Tous les membres de l'instance ne sont pas garantis comme sûrs.
Plateformes
Plateformes de développement
Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Informations sur la version
.NET Framework et NET Compact Framework
Pris en charge dans 3.5
.NET Framework
Pris en charge dans 3.0
.NET Compact Framework et .Net Framework
Pris en charge dans 2.0
Voir aussi
Référence
SqlCeDataReader Class
SqlCeDataReader Members
System.Data.SqlServerCe Namespace