Méthode SqlCeCommand.SetRange
Restreint le jeu de lignes qui sera lu par SqlCeDataReader.
Espace de noms : System.Data.SqlServerCe
Assembly : System.Data.SqlServerCe (en System.Data.SqlServerCe.dll)
Syntaxe
'Déclaration
Public Sub SetRange ( _
dbRangeOptions As DbRangeOptions, _
startData As Object(), _
endData As Object() _
)
'Utilisation
Dim instance As SqlCeCommand
Dim dbRangeOptions As DbRangeOptions
Dim startData As Object()
Dim endData As Object()
instance.SetRange(dbRangeOptions, startData, _
endData)
public void SetRange(
DbRangeOptions dbRangeOptions,
Object[] startData,
Object[] endData
)
public:
void SetRange(
DbRangeOptions dbRangeOptions,
array<Object^>^ startData,
array<Object^>^ endData
)
member SetRange :
dbRangeOptions:DbRangeOptions *
startData:Object[] *
endData:Object[] -> unit
public function SetRange(
dbRangeOptions : DbRangeOptions,
startData : Object[],
endData : Object[]
)
Paramètres
- dbRangeOptions
Type : System.Data.SqlServerCe.DbRangeOptions
Options utilisées lors de la spécification de la plage.
- startData
Type : array<System.Object[]
Valeurs de clés de début de la plage.
- endData
Type : array<System.Object[]
Valeurs de clés de fin de la plage.
Exceptions
Exception | Condition |
---|---|
InvalidOperationException | La propriété IndexName n'a pas été définie. |
Notes
Cette méthode doit normalement constituer une solution de remplacement plus rapide que l'instruction SELECT pour la récupération d'un jeu de lignes à partir d'une table de base. À la place d'une clause WHERE dans une instruction SELECT, vous pouvez utiliser SetRange pour récupérer rapidement un jeu de lignes en fonction de leurs valeurs d'index. Par exemple, pour récupérer un jeu d'employés ayant un ID d'employé compris entre 1 et 5, vous pouvez exécuter une instruction SELECT, mais la définition d'une plage comprise entre 1 et 5 sur l'index d'ID des employés améliore considérablement les performances.
Cette méthode peut être utilisée uniquement quand CommandType a la 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. Lorsque SetRange est utilisé, le SqlCeDataReader retourné par ExecuteReader retourne uniquement les lignes dont les valeurs de clés dans l'index spécifié correspondent à la plage.
Lorsque Seek est utilisé sur SqlCeDataReader possédant une plage, Seek se positionne uniquement sur les lignes se situant dans la plage spécifiée. Pour plus d'informations sur SetRange, consultez la rubrique « IRowsetIndex::SetRange » dans la documentation OLE DB.
Exemples
Cet exemple récupère des données de la table Orders à l'aide d'un index dans SqlCeDataReader. La plage d'index sur laquelle effectuer Seek est spécifiée par la méthode SetRange.
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);
}