Table Maintenance |
|
If you happen to have a table you don't need anymore or whose role is undefined in your application, you can delete that table. This operation is supported by the DataTableCollection.Remove() method that is overloaded with two versions. To delete a table using its variable declared name, you can use the following version: public void Remove(DataTable table); This version expects the name that was used to declare the DataTable object. If the table exists in the DateSet.Tables collection, it would be deleted. Here is an example: using System; using System.Data; public class VideoCollection { . . . No Change public void ShowTables() { DataTableCollection tables = this.dsVideoCollection.Tables; foreach (DataTable tbl in tables) Console.WriteLine("Table Name: {0}", tbl.TableName); dsVideoCollection.Tables.Remove(tblVideoCategories); Console.WriteLine(); foreach (DataTable tbl in tables) Console.WriteLine("Table Name: {0}", tbl.TableName); } } public static class Program { static int Main(string[] args) { VideoCollection coll = new VideoCollection(); coll.ShowTables(); Console.WriteLine(); return 0; } } To delete a table using its object name, you can use the following version of the DataTableCollection.Remove() method: public void Remove(string name); This method expects the formal name of the table as argument. If a table exists under that name, it would be deleted. Here is an example: using System; using System.Data; public class VideoCollection { . . . No Change public void ShowTables() { DataTableCollection tables = this.dsVideoCollection.Tables; foreach (DataTable tbl in tables) Console.WriteLine("Table Name: {0}", tbl.TableName); dsVideoCollection.Tables.Remove("Categories"); Console.WriteLine(); foreach (DataTable tbl in tables) Console.WriteLine("Table Name: {0}", tbl.TableName); } } public static class Program { static int Main(string[] args) { VideoCollection coll = new VideoCollection(); coll.ShowTables(); Console.WriteLine(); return 0; } } This would produce: Table Name: Directors Table Name: Categories Table Name: Ratings Table Name: Actors Table Name: Formats Table Name: Directors Table Name: Ratings Table Name: Actors Table Name: Formats Press any key to continue . . . If no table with the name is found, the compiler would throw an ArgumentException exception. Once again, you should first check that a table with the undesired name exist before deleting it. If the table exists in the collection, it may not allow the user to delete it. To find out whether a table can be deleted, call the DataTableCollection.CanRemove() method. Its syntax is: public bool CanRemove(DataTable table);
When calling the DataTableCollection.Remove() method, if the DataTable object passed as argument is not found, the compiler would throw either an ArgumentNullException or an ArgumentException exceptions. For this reason, before deleting a table, you should first check its existence. To do this, you can call the DataTableCollection.Contains() method. Here is an example of calling this method before deleting a table: using System; using System.Data; namespace VideoCollection1 { public class Video { public DataSet dsVideoCollection; public DataTable tblVideoCategories; public DataTable tblDirectors; public DataTable tblRatings; public DataTable tblActors; public DataTable tblFormats; public Video() { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add("Ratings"); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); } public void ShowTables() { int i = 1; Console.WriteLine("Video Collection - Tables"); foreach (DataTable tbl in dsVideoCollection.Tables) Console.WriteLine("{0}. {1}", i++, tbl.TableName); } public void DeleteTable(string name) { if (dsVideoCollection.Tables.Contains(name)) dsVideoCollection.Tables.Remove(name); else Console.WriteLine("Table {0} not found in the database", name); } public void PerformMaintenance() { ShowTables(); Console.WriteLine(); DeleteTable("Ratings"); Console.WriteLine(); ShowTables(); Console.WriteLine(); DeleteTable("Types"); Console.WriteLine(); ShowTables(); Console.WriteLine(); } } } This would produce: Video Collection - Tables 1. Directors 2. Categories 3. Ratings 4. Actors 5. Formats Video Collection - Tables 1. Directors 2. Categories 3. Actors 4. Formats Table Types not found in the database Video Collection - Tables 1. Directors 2. Categories 3. Actors 4. Formats Press any key to continue . . .
To delete all tables of a DataSet object, you can call the DataTableCollection.Clear() method. Its syntax is: public void Clear(); Calling this method would remove all DataTable objects of the DataSet.
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|