How to clone DataTable structure excluding all data, schemas and data table constraints?

Hi,
After calling the SQL query by Execute Query Activity
image

the result is stored in System.Data.DataTable, which inherits the column schema from core SQL Data Base, eg. DateTime, Double.

I want to clone this table to a new one:
The new table is to contain the general layout of the old table, that is: all columns and their names.

The new table must not contain restrictions on the data type it stores and values.

Visualization:
From this:
image

To this:
image

How can this be achieved?

I need to make changes to the data in the table. For example, convert Double (0,155) to a value expressed in % (15,5%), change the date format, etc.

After changes: new table will looks like:
image

@Adrian_Star
newDT = OldDT.Clone
afterwards you can finetune / adopt the cloned Datatable

as it looks that you are only interested on the columnnames as alternate do following:

For each activity - TypeArgument: String - Values: oldDT.Columns.Cast(of DataColumn).Select(Function (x) x.ColumnName).toArray

so you will iterate over the names and can used within an add DataColumn on a new DataTable

I’ve tried, but Clone clones the structure of the DataTable, including all DataTable schemas and constraints.

After Clone old data table to new one I have error:

RemoteException wrapping System.ArgumentException: Input string was not in a correct format.Couldn't store <15%> in Dbl Column.  Expected type is Double.  ---> RemoteException wrapping System.FormatException: Input string was not in a correct format. 

I also tried:

.DefaultView().ToTable()

and it doesn’t work either.

please read again the post and give a try on second option.
DataTableVar.Clone will keep the datatype

Works!
Is there any function / method or LINQ available to not do it in a for each loop?

DT_Old.Columns.Cast(of DataColumn).Select(Function (x) x.ColumnName).toArray

it could look like this:

Variables:
grafik

flow:
grafik

Result:
grafik

Kindly note: this approach risks to fall into a blackbox case and it is up to your code guideline / development rules to use it or not.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.