|
Localiser des enregistrements et leurs valeurs |
|
|
Localiser un enregistrement |
|
Avant d'effectuer n'importe quelle opération sur un enregistrement, vous devez pouvoir le localiser. C'est-à-dire, vous devez pouvoir identifier un enregistrement parmi les divers enregistrements d'un tableau. Pour localiser un enregistrement dans la collection DataTable.Rows, la classe DataRowCollection a une propriété indexée qui est définie comme suit : public DataRow this[int index] {get;}
Les enregistrements d'un tableau sont stockés dans une liste (appelée DataRowCollection). Le premier enregistrement a un index 0. Le deuxième enregistrement a un index 1, et ainsi de suite. Voici un exemple à utiliser pour recouvrer l'information stockée dans un enregistrement: |
using System;
using System.IO;
using System.Data;
namespace VideoCollection
{
public static class Program
{
. . . No Change
static void ShowVideos()
{
dsVideos.ReadXml(strDirectory + @"\videos.xml");
Console.WriteLine("Video Collection");
Console.WriteLine("================================");
foreach (DataRow row in tblVideos.Rows)
{
foreach (DataColumn col in tblVideos.Columns)
{
Console.WriteLine("{0}", row[col]);
}
Console.WriteLine("--------------------------------");
}
}
static int Main(string[] args)
{
CreateCollection();
ShowVideos();
return 0;
}
}
}
Ceci produirait: Video Collection ================================ GT-682 A Few Good Men Rob Reiner 138 Minutes 1992 R -------------------------------- MM-258 Fatal Attraction Adrian Lyne 120 Minutes 1987 R -------------------------------- FD-205 Her Alibi Bruce Beresford 94 Minute 1989 PG-13 -------------------------------- Press any key to continue . . . La classe DataRow elle-même est équipée d'une propriété ingexée qui vous permet d'accéder à la valeur stockée dans une colonne particulière. Par exemple, vous pouvez utiliser la boucle for pour visiter chaque colonne par son index. Une fois que vous obtenez une colonne, vous pouvez alors utiliser la propriété indexée d'une rangée pour accéder à la valeur stockée sous cette colonne. Voici les exemples: using System;
using System.IO;
using System.Data;
namespace VideoCollection
{
public static class Program
{
. . . No Change
static void ShowVideos()
{
dsVideos.ReadXml(strDirectory + @"\videos.xml");
Console.WriteLine("================================");
Console.WriteLine("Video Collection");
Console.WriteLine("================================");
for (int i = 0; i < tblVideos.Rows.Count; i++)
{
DataRow row = tblVideos.Rows[i];
Console.WriteLine("Shelf #: {0}", tblVideos.Rows[i]["ShelfNumber"]);
Console.WriteLine("Title: {0}", tblVideos.Rows[i]["Title"]);
Console.WriteLine("Director: {0}", tblVideos.Rows[i]["Director"]);
Console.WriteLine("Length: {0}", tblVideos.Rows[i]["Length"]);
Console.WriteLine("Year: {0}", tblVideos.Rows[i]["Year"]);
Console.WriteLine("Rating: {0}", tblVideos.Rows[i]["Rating"]);
Console.WriteLine("--------------------------------");
}
}
static int Main(string[] args)
{
CreateCollection();
ShowVideos();
return 0;
}
}
}
Ceci produirait: ================================ Video Collection ================================ Shelf #: GT-682 Title: A Few Good Men Director: Rob Reiner Length: 138 Minutes Year: 1992 Rating: R -------------------------------- Shelf #: MM-258 Title: Fatal Attraction Director: Adrian Lyne Length: 120 Minutes Year: 1987 Rating: R -------------------------------- Shelf #: FD-205 Title: Her Alibi Director: Bruce Beresford Length: 94 Minute Year: 1989 Rating: PG-13 -------------------------------- Press any key to continue . . . En utilisant une quelconque d'entre les techniques précédentes (for ou foreach), si vous indiquez un index qui est soit inférieur 0 ou au delà du nombre d'enregistrements dans le tableau, le compilateur rejetterait une exception IndexOutOfRangeException.
Comme mentionné déjà, un enregistrement est en fait une ou plusieurs valeurs de chacune des colonnes du tableau. Considérez le tableau suivant:
Pour localiser la valeur qu'un enregistrement con tient sous une colonne particulière, la classe DataRow a une propriété indexée qui est prise en charge dans diverses versions (réellement six, mais actuellement nous sommes intéressés par les trois premières seulement). Une des versions de cette propriété utilise la syntaxe suivante : public object this[string columnName] {get; set;}
Pour utiliser cette propriété, passez le nom de l'objet de la colonne dans les crochets. Voici les exemples: using System;
using System.IO;
using System.Data;
namespace VideoCollection
{
public static class Program
{
. . . No Change
static void ShowVideos()
{
dsVideos.ReadXml(strDirectory + @"\videos.xml");
Console.WriteLine("================================");
Console.WriteLine("Video Collection");
Console.WriteLine("================================");
for (int i = 0; i < tblVideos.Rows.Count; i++)
{
DataRow row = tblVideos.Rows[i];
Console.WriteLine("Shelf #: {0}",
tblVideos.Rows[i][colShelfNumber]);
Console.WriteLine("Title: {0}", tblVideos.Rows[i][colTitle]);
Console.WriteLine("Director: {0}", tblVideos.Rows[i][colDirector]);
Console.WriteLine("Length: {0}", tblVideos.Rows[i][colLength]);
Console.WriteLine("Year: {0}", tblVideos.Rows[i][colYear]);
Console.WriteLine("Rating: {0}", tblVideos.Rows[i][colRating]);
Console.WriteLine("--------------------------------");
}
}
static int Main(string[] args)
{
CreateCollection();
ShowVideos();
return 0;
}
}
}
Au lieu d'utiliser l'index d'une colonne, vous pouvez également localiser une valeur en utilisant le nom de la variable de sa colonne. Pour ce faire, vous pouvez utiliser la syntaxe suivante de la propriété indexée DataRow : public object this[DataColumn column] {get; set;}
Cette propriété s'attend au nom d'objet de la colonne passée dans ses crochets. Nous avons vu plus tôt comment utiliser cette version de la propriété. Voici les exemples, en utilisant foreach: using System;
using System.IO;
using System.Data;
namespace VideoCollection
{
public static class Program
{
. . . No Change
static void ShowVideos()
{
dsVideos.ReadXml(strDirectory + @"\videos.xml");
Console.WriteLine("================================");
Console.WriteLine("Video Collection");
Console.WriteLine("================================");
foreach (DataRow row in tblVideos.Rows)
{
Console.WriteLine("Shelf #: {0}", row["ShelfNumber"]);
Console.WriteLine("Title: {0}", row["Title"]);
Console.WriteLine("Director: {0}", row["Director"]);
Console.WriteLine("Length: {0}", row["Length"]);
Console.WriteLine("Year: {0}", row["Year"]);
Console.WriteLine("Rating: {0}", row["Rating"]);
Console.WriteLine("--------------------------------");
}
}
static int Main(string[] args)
{
CreateCollection();
ShowVideos();
return 0;
}
}
}
La troisième option que vous avez est d'identifier la colonne par son index. Pour ce faire, utilisez la syntaxe suivante de la propriété indexée DataRow : public object this[int columnIndex] {get; set;}
Cette propriété s'attend à l'index de la colonne. Voici des exemples qui utilisent la boucle for: using System;
using System.IO;
using System.Data;
namespace VideoCollection
{
public static class Program
{
. . . No Change
static void ShowVideos()
{
dsVideos.ReadXml(strDirectory + @"\videos.xml");
Console.WriteLine("================================");
Console.WriteLine("Video Collection");
Console.WriteLine("================================");
for (int i = 0; i < tblVideos.Rows.Count; i++)
{
DataRow row = tblVideos.Rows[i];
Console.WriteLine("Shelf #: {0}", tblVideos.Rows[i][0]);
Console.WriteLine("Title: {0}", tblVideos.Rows[i][1]);
Console.WriteLine("Director: {0}", tblVideos.Rows[i][2]);
Console.WriteLine("Length: {0}", tblVideos.Rows[i][3]);
Console.WriteLine("Year: {0}", tblVideos.Rows[i][4]);
Console.WriteLine("Rating: {0}", tblVideos.Rows[i][5]);
Console.WriteLine("--------------------------------");
}
}
static int Main(string[] args)
{
CreateCollection();
ShowVideos();
return 0;
}
}
}
Ou, ici des exemples qui utilisent foreach: using System;
using System.IO;
using System.Data;
namespace VideoCollection
{
public static class Program
{
. . . No Change
static void ShowVideos()
{
dsVideos.ReadXml(strDirectory + @"\videos.xml");
Console.WriteLine("================================");
Console.WriteLine("Video Collection");
Console.WriteLine("================================");
foreach (DataRow row in tblVideos.Rows)
{
Console.WriteLine("Shelf #: {0}", row[0]);
Console.WriteLine("Title: {0}", row[1]);
Console.WriteLine("Director: {0}", row[2]);
Console.WriteLine("Length: {0}", row[3]);
Console.WriteLine("Year: {0}", row[4]);
Console.WriteLine("Rating: {0}", row[5]);
Console.WriteLine("--------------------------------");
}
}
static int Main(string[] args)
{
CreateCollection();
ShowVideos();
return 0;
}
}
}
|
|
|
||
| Précédent | Copyright © 2007, Yevol | Suivant |
|
|
||