Reputation: 55
since I'm very new in C#, i wanted to know which is a good option for multiple datatables management. I have a project with 100 datatables circa (let's say similar to each other but different in columns number). I don't know how to collect them, do I need an array of datatables? (ps: datatables are created dinamically at runtime and the number of them may vary) thank you in advance
Upvotes: 1
Views: 2292
Reputation: 29036
You can use Dataset, which itself is a collection of DataTable objects. so you can add/mange/delete datatables from it. DataSets can hold multiple tables and you can define relationships between those tables.
Let dataContainer
be a dataset defined as below:
DataSet dataContainer = new DataSet();
Consider the methods addtableToSet()
which will show how to add datatables to the DataSet, and RemoveTableFromSet()
will show you how to remove them based on name.
public void addtableToSet()
{
DataTable tableA = new DataTable("TableA");
DataTable tableB = new DataTable("TableB");
DataTable tableC = new DataTable("TableC");
dataContainer.Tables.Add(tableA);
dataContainer.Tables.Add(tableB);
dataContainer.Tables.Add(tableC);
dataContainer.Tables.Add(new DataTable("TableD"));
}
public void RemoveTableFromSet(string tableName)
{
dataContainer.Tables.Remove(tableName);
}
Upvotes: 0
Reputation: 10055
It all depends on what you want to do with the datatable once they are generated.
You could keep them all in a dictionary if you can have a unique name for each datatable.
DataTable GeneratedTable1;
DataTable GeneratedTable2;
Dictionary<string, DataTable> DataTableDictionary = new Dictionary<string, DataTable>();
// Adding the tables to the Dictionary
DataTableDictionary.Add("MyUniqueName1", GeneratedTable1);
DataTableDictionary.Add("MyUniqueName2", GeneratedTable2);
// Lookup a DataTable
DataTable MyLookedUpDataTable = DataTableDictionary["MyUniqueName1"];
Upvotes: 0
Reputation: 11399
In this case you can use the DataSet. With this class you can collect many DataTables and work with them. DataTables are stored in the DataSet.Tables property.
A Dataset is like a database ... containing many tables with relationsships an so on.
Upvotes: 1