|
Gestion des Enregistrements |
|
Une fois qu'un tableau a été remplie d'enregistrements, vous pouvez effectuer des opérations d'entretien dessus telle que changer quelques enregistrements ou enlever d'autres. L'édition d'un enregistrement consiste à changer une des valeurs de l'enregistrement sous une colonne particulière. Il y a diverses manières de faire ceci. Pour une application de console, les étapes générales que vous pouvez (nous pourrons) suivre sont :
Pour exécuter ces étapes, vous utilisez une combinaison des techniques que nous avons passées en revue jusqu'ici : localiser le tableau, affiche les enregistrements, localiser l'enregistrement, localiser la colonne, assigner la valeur à la colonne d'un enregistrement, sauvegarder le tableau.
Dans la leçon précédente, nous avons vu que vous pourriez isoler un enregistrement basé sur les noms d'objet des colonnes tels que Le Producteur ou le Titre pour un tableau des videos. Une fois que vous avez identifié une colonne pour un enregistrement, vous pouvez assigner la valeur désirée. Voici les exemples: using System;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections;
namespace VideoCollection3
{
public class VideoCollection
{
public DataSet dsVideos;
private DataColumn colShelfNumber;
private DataColumn colTitle;
private DataColumn colDirector;
private DataColumn colYear;
private DataColumn colLength;
private DataColumn colRating;
private DataTable tblVideos;
// These are accessory strings
string strDirectory;
string strFilename;
public VideoCollection()
{
CreateCollection();
strDirectory = @"C:\Programs\Video Collection";
strFilename = strDirectory + "\\" + "videos.xml";
DirectoryInfo dirInfo = new DirectoryInfo(strDirectory);
// If the folder doesn't exist already, create it
if (!dirInfo.Exists)
dirInfo.Create();
}
public void CreateCollection()
{
dsVideos = new DataSet("Videos");
tblVideos = new DataTable("Video");
colShelfNumber = new DataColumn("ShelfNumber",
Type.GetType("System.String"));
tblVideos.Columns.Add(colShelfNumber);
colTitle = new DataColumn("Title", Type.GetType("System.String"));
tblVideos.Columns.Add(colTitle);
colDirector = new DataColumn("Director",
Type.GetType("System.String"));
tblVideos.Columns.Add(colDirector);
colYear = new DataColumn("Year", Type.GetType("System.Int32"));
tblVideos.Columns.Add(colYear);
colLength = new DataColumn("Length", Type.GetType("System.String"));
tblVideos.Columns.Add(colLength);
colRating = new DataColumn("Rating", Type.GetType("System.String"));
tblVideos.Columns.Add(colRating);
dsVideos.Tables.Add(tblVideos);
}
public void ShowTables()
{
int i = 1;
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
Console.WriteLine("Video Collection - Tables");
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
foreach (DataTable tbl in dsVideos.Tables)
Console.WriteLine("{0}. {1}", i++, tbl.TableName);
Console.WriteLine("----------------------------");
}
public void ShowColumns(string table)
{
int i = 1;
DataTable tbl = dsVideos.Tables[table];
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
Console.WriteLine("Video Collection - {0} Columns", table);
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
foreach (DataColumn col in tbl.Columns)
Console.WriteLine("{0}. {1}", i++, col.ColumnName);
Console.WriteLine("----------------------------");
}
public void CreateVideo()
{
string ShelfNumber, Title, Director, Length;
int Year;
string Rating;// = { "G", "PG", "PG-13", "R", "NC-17", "N/R" };
Console.WriteLine(
"Enter the following pieces of information about the video");
Console.Write("Shelf Number: ");
ShelfNumber = Console.ReadLine();
XmlDocument xmlVideos = new XmlDocument();
if (File.Exists(strFilename))
{
xmlVideos.Load(strFilename);
dsVideos.ReadXml(strFilename);
XmlElement nodRoot = xmlVideos.DocumentElement;
XmlNodeList nodShelfNumbers =
nodRoot.GetElementsByTagName("ShelfNumber");
foreach (XmlNode nodShelfNumber in nodShelfNumbers)
{
if (nodShelfNumber.InnerText == ShelfNumber)
{
Console.WriteLine(
"The shelf number {0} exists already",
ShelfNumber);
return;
}
}
}
else
dsVideos.WriteXml(strFilename);
Console.Write("Title: ");
Title = Console.ReadLine();
Console.Write("Director: ");
Director = Console.ReadLine();
Console.Write("Year Released: ");
Year = int.Parse(Console.ReadLine());
Console.Write("Length (ex 118mins): ");
Length = Console.ReadLine();
Console.WriteLine("Specify corresponding rating");
Console.WriteLine("G");
Console.WriteLine("PG");
Console.WriteLine("PG-13");
Console.WriteLine("R");
Console.WriteLine("NC-17");
Console.WriteLine("N/R");
Console.Write("Your Choice? ");
Rating = Console.ReadLine();
DataRow rowVideo = this.tblVideos.NewRow();
rowVideo[0] = ShelfNumber;
rowVideo[1] = Title;
rowVideo[2] = Director;
rowVideo[3] = Year;
rowVideo[4] = Length;
rowVideo[5] = Rating;
this.tblVideos.Rows.Add(rowVideo);
this.dsVideos.WriteXml(strFilename);
}
public void ShowVideos()
{
if (File.Exists(strFilename))
{
dsVideos.ReadXml(strFilename);
Console.WriteLine("========================================");
Console.WriteLine(" Video Collection");
Console.WriteLine("=========================================");
foreach (DataRow vdo in tblVideos.Rows)
{
Console.WriteLine("Shelf #: {0}", vdo["ShelfNumber"]);
Console.WriteLine("Title: {0}", vdo["Title"]);
Console.WriteLine("Director: {0}", vdo["Director"]);
Console.WriteLine("(c) Year: {0}", vdo["Year"]);
Console.WriteLine("Length: {0:C}", vdo["Length"]);
Console.WriteLine("Rating: {0}", vdo["Rating"]);
Console.WriteLine("-----------------------------------------");
}
}
}
public void EditVideo()
{
if (File.Exists(strFilename))
{
bool found = false;
int iYear = 0;
string strShelfNumber = "AA-000",
strTitle = "Unknown",
strDirector = "Unknown",
strLength = "N/A",
strRating = "N/A";
dsVideos.ReadXml(strFilename);
DataRow rowVideo = null;
Console.WriteLine("\nHere is the current list of videos");
Console.WriteLine("========================================");
Console.WriteLine(" Video Collection");
Console.WriteLine("=========================================");
foreach (DataRow vdo in tblVideos.Rows)
{
Console.WriteLine("Shelf #: {0}", vdo["ShelfNumber"]);
Console.WriteLine("Title: {0}", vdo["Title"]);
Console.WriteLine("Director: {0}", vdo["Director"]);
Console.WriteLine("(c) Year: {0}", vdo["Year"]);
Console.WriteLine("Length: {0:C}", vdo["Length"]);
Console.WriteLine("Rating: {0}", vdo["Rating"]);
Console.WriteLine("-----------------------------------------");
}
Console.Write(
"Enter the shelf number of the video you want to edit: ");
strShelfNumber = Console.ReadLine();
foreach (DataRow vdo in tblVideos.Rows)
{
string str = (string)vdo["ShelfNumber"];
Console.WriteLine(str);
if( str == strShelfNumber )
{
//rowVideo = vdo;
found = true;
Console.WriteLine(
"\n-----------------------------------------");
Console.WriteLine("Here is the video");
Console.WriteLine("1. Title: {0}", vdo["Title"]);
Console.WriteLine("2. Director: {0}", vdo["Director"]);
Console.WriteLine("3. (c) Year: {0}", vdo["Year"]);
Console.WriteLine("4. Length: {0}", vdo["Length"]);
Console.WriteLine("5. Rating: {0}", vdo["Rating"]);
Console.WriteLine(
"-----------------------------------------");
strTitle = (string)vdo["Title"];
strDirector = (string)vdo["Director"];
iYear = (int)vdo["Year"];
strLength = (string)vdo["Length"];
strRating = (string)vdo["Rating"];
Console.Write("Enter the index of the column " +
"whose value you want to change: ");
int col = int.Parse(Console.ReadLine());
switch (col)
{
case 1:
vdo["ShelfNumber"] = strShelfNumber;
Console.Write("Enter the new video title: ");
strTitle = Console.ReadLine();
vdo["Title"] = strTitle;
vdo["Director"] = strDirector;
vdo["Year"] = iYear;
vdo["Length"] = strLength;
vdo["Rating"] = strRating;
this.dsVideos.WriteXml(strFilename);
break;
case 2:
vdo["ShelfNumber"] = strShelfNumber;
vdo["Title"] = strTitle;
Console.Write(
"Enter the new director of the video: ");
strDirector = Console.ReadLine();
vdo["Director"] = strDirector;
vdo["Year"] = iYear;
vdo["Length"] = strLength;
vdo["Rating"] = strRating;
this.dsVideos.WriteXml(strFilename);
break;
case 3:
vdo["ShelfNumber"] = strShelfNumber;
vdo["Title"] = strTitle;
vdo["Director"] = strDirector;
Console.Write(
"Enter the right year released of the video: ");
iYear = int.Parse( Console.ReadLine());
vdo["Year"] = iYear;
vdo["Length"] = strLength;
vdo["Rating"] = strRating;
this.dsVideos.WriteXml(strFilename);
break;
case 4:
vdo["ShelfNumber"] = strShelfNumber;
vdo["Title"] = strTitle;
vdo["Director"] = strDirector;
vdo["Year"] = iYear;
Console.Write(
"Enter the new length of the video: ");
strLength = Console.ReadLine();
vdo["Length"] = strLength;
vdo["Rating"] = strRating;
this.dsVideos.WriteXml(strFilename);
break;
case 5:
vdo["ShelfNumber"] = strShelfNumber;
vdo["Title"] = strTitle;
vdo["Director"] = strDirector;
vdo["Year"] = iYear;
vdo["Length"] = strLength;
Console.Write(
"Enter the right rating for the video: ");
strRating = Console.ReadLine();
vdo["Rating"] = strRating;
this.dsVideos.WriteXml(strFilename);
break;
}
return;
}
}
if (found == false)
{
Console.WriteLine("No video with that shelf number was found");
return;
}
}
}
}
}
Nous avons vu qu'une autre technique d'identifier un enregistrement était en utilisant l'index de chaque colonne appliquée à l'objet DataRow de l'enregistrement. Vous pouvez appliquer ce concept pour identifier chaque colonne. Une fois que vous l'avez fait, vous pouvez alors assigner la valeur désirée. |
|
|
||
| Précédent | Copyright © 2007, Yevol | Suivant |
|
|
||