Datatable for a class

Asked

Viewed 57 times

3

I am a beginner in c# and right now I have some doubts about how to pass the structure of a datatable for a class to later use it, this to avoid duplicate code since I have several tables that use the same structure depending on the type (condition that has to be used A or B). and was trying to pass these structures to a class in which there would be a class that contains Structure for Type A ( datable structure and datarows) and B the same thing. I just don’t know how to pass one datatable for a class and then call in the main.

How can I pass this logic (Table structure ) to a class and call it in the main?

Datatable structure (example) :

nomeTabela.columns.add("...")

(...)

DataRow drinfo = dataTableInfo.NewRow();

  drinfo ["col1"] = var1;

  drinfo ["col2"] = var2;

  drinfo ["col3"] = var3; 

  drinfo ["col4"] = var4;

  drinfo ["col5"] = var5;

  drinfo ["col6"] = var6;

(...)

 datatableX.Rows.Add(drinfo );
  • 1

    Do not use Datatable, search by repository pattern and be happy

1 answer

2

You are looking for the method Datatable.() which returns the structural clone of an object DataTable, including all schemes and restrictions assigned to that DataTable.

Use:

After defining the structure of a table, here called tabelaOriginal just evoke your method clone().

DataTable tabelaClonada1 = tabelaOriginal.Clone();
DataTable tabelaClonada2 = tabelaOriginal.Clone();
DataTable tabelaClonada3 = tabelaOriginal.Clone();
// ...
DataTable tabelaClonadaN = tabelaOriginal.Clone();

Thus allowing you to create a global table where you can clone the structure in the part of the program that suits you.

As the purpose of the question is to obtain information about replication methods of the structure of a DataTable, inform that it is also possible to save the structure of a table in Xml for later use with DataTable.WriteXmlSchema(string arquivo), which can be read using the DataTable.ReadXmlSchema(string arquivo).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.