1

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

1
  • thanks to all for all answers, now i know what to do. i'm still getting confused tough, because n addition to your suggested code (which works fine for me), in visual studio I see ad add->new item->dataset and it create a whole bunch of files like a .xsd and others. is that a dataset too? it's a different way of implementing it? or it's related to other functionalities? thank you very much for your help Commented Apr 22, 2016 at 7:43

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"];

Comments

0

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); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.