dataset cloning

  • Thread starter Thread starter Dmitry Karneev
  • Start date Start date
D

Dmitry Karneev

Hi!

I have a rather interesting problem.
I have a dataset that contatin data from database.
I need to clone this dataset with data but all id's in tables must be NULL
and all DataRowStates must be Added because I want to make some changes to
dataset and then insert it to database as new records.
How can I do it?

Thanks in advance,
Dmitry
 
Dmitry,

If I understand correctly, you want to take the rows that you have in a
DataTable, and null out some ids, and add them back into the database. What
I would do in this create a blank copy (schema only) of the table, and then
cycle through the rows in the old table. I would copy over the values, row
by row, column by column to the new table, omitting the id field (because it
needs to be null).

Then, you can pass that table to a data adapter, and you should be able
to save the new values to the table.

Hope this helps.
 
Thanks for advice, Nicholas!

I worried that it might be the only way to solve my problem.
It would take much time to complete and I was looking for some quicker
solution.

Is it possiple to change rowstate to Added by some trick?

Nicholas Paldino said:
Dmitry,

If I understand correctly, you want to take the rows that you have in a
DataTable, and null out some ids, and add them back into the database. What
I would do in this create a blank copy (schema only) of the table, and then
cycle through the rows in the old table. I would copy over the values, row
by row, column by column to the new table, omitting the id field (because it
needs to be null).

Then, you can pass that table to a data adapter, and you should be able
to save the new values to the table.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dmitry Karneev said:
Hi!

I have a rather interesting problem.
I have a dataset that contatin data from database.
I need to clone this dataset with data but all id's in tables must be NULL
and all DataRowStates must be Added because I want to make some changes to
dataset and then insert it to database as new records.
How can I do it?

Thanks in advance,
Dmitry
 
Dmitry,

No, it is not possible, not in .NET 1.1. However, you will be able to
do it in .NET 2.0.

Honestly though, it's not that hard to do what I mentioned. Basically,
you could do this:

// Assume originalTable is the DataTable. Copy the data table.
DataTable copy = originalTable.Clone();

// Now, clear the data.
copy.Clear();

// Get the ordinal of the column that has the id field.
int idOrdinal = copy.Columns["id"].Ordinal;

// The array that has the values in the data.
object[] itemData = null;

// Cycle through the rows in the original.
foreach (DataRow row in originalTable)
{
// Get the item data.
itemData = row.ItemArray;

// Set the id column to null.
itemData[idOrdinal] = null;

// Now add the row to the copied table.
copy.Rows.Add(itemData);
}

At this point, you can just pass the copy to a data adapter, and it
should add all of your new rows. You don't want to use ImportRow in this
case, because you wouldn't get an add state which would trigter an insert
into your db.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dmitry Karneev said:
Thanks for advice, Nicholas!

I worried that it might be the only way to solve my problem.
It would take much time to complete and I was looking for some quicker
solution.

Is it possiple to change rowstate to Added by some trick?

in
message news:[email protected]...
Dmitry,

If I understand correctly, you want to take the rows that you have in a
DataTable, and null out some ids, and add them back into the database. What
I would do in this create a blank copy (schema only) of the table, and then
cycle through the rows in the old table. I would copy over the values, row
by row, column by column to the new table, omitting the id field (because it
needs to be null).

Then, you can pass that table to a data adapter, and you should be able
to save the new values to the table.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dmitry Karneev said:
Hi!

I have a rather interesting problem.
I have a dataset that contatin data from database.
I need to clone this dataset with data but all id's in tables must be NULL
and all DataRowStates must be Added because I want to make some changes to
dataset and then insert it to database as new records.
How can I do it?

Thanks in advance,
Dmitry
 
Back
Top