Copy DataSet questions

B

Brent

Hello,

I am about to start developing some code that will take a complex DataSet
and Copy it to a new DataSet. This new dataset must be recognized by our web
service as a completely new entity so any primary keys that are autonumbers
must be changed to negative numbers. I also need the rowstate for each row
in each table in the DataSet to equal DataRowState.Added so that the save
function on the web service treats every row in the DataSet as a new row.

Here's what I'm thinking:

- Copy the data structure only to the new DataSet with the MyDataSet.Clone()
method
- Loop through all the tables and rows and insert them into the new DataSet
- Because I'm adding rows to a new empty DataSet I'm hoping that the
DataRowState property is set to 'Added'
- Loop through all the newly created rows in the new DataSet and change all
primary keys to a negative number

If I send this dataset into my web service I'm hoping that it is recognized
as a completely new dataset so that all rows are inserted into the database.

Am I on the right track here or is there an easier way to do this.

Thanks,

Brent...
 
M

Miha Markic [MVP C#]

Hi Brent,

Brent said:
Hello,

I am about to start developing some code that will take a complex DataSet
and Copy it to a new DataSet. This new dataset must be recognized by our
web
service as a completely new entity so any primary keys that are
autonumbers
must be changed to negative numbers. I also need the rowstate for each row
in each table in the DataSet to equal DataRowState.Added so that the save
function on the web service treats every row in the DataSet as a new row.

Here's what I'm thinking:

- Copy the data structure only to the new DataSet with the
MyDataSet.Clone()
method
- Loop through all the tables and rows and insert them into the new
DataSet
- Because I'm adding rows to a new empty DataSet I'm hoping that the
DataRowState property is set to 'Added'

Yes, it will.
- Loop through all the newly created rows in the new DataSet and change
all
primary keys to a negative number

Not necessary as it won't let you enter a primary key at all (and you
shouldn't).
If you set the pk to autoincrement with seed and step to -1 it will be just
fine.
If I send this dataset into my web service I'm hoping that it is
recognized
as a completely new dataset so that all rows are inserted into the
database.

It will see that rows are new (Added) and will insert them probably -
depends on what exactly it does.
Am I on the right track here or is there an easier way to do this.

An interesting option is DataAdapter.AcceptChangesDuringFill which will
leave rows as Added when filled from database.
If this helps you...
 
B

Brent

Hello Miha,

That last bit of advice about DataAdapter.AcceptChangesDuringFill is
helpful.

Thanks,

Brent...
 
B

Brent

This is actually proving to be a lot more difficult than I first thought. My
dataset is disconnected and I have over 30 tables in it all many of them are
related to other tables via foreign keys that need to change to negative
numbers so that they will be replaced with valid PKs when they are inserted
into the database.

I'm starting think I should export the dataset to an XML file and make the
changes manually then import them into the new dataset.

Basically what I'm trying to do is take a snapshot of a dataset and then
send that back to the database so that each record in each table is treated
like a new row in the database. I have a field in the new dataset that holds
the primary key of the root original dataset so that I can get a list of all
snapshots that are attched to a dataset.

I looked into DataAdapter.Fill and DataAdapter.AcceptChangesDuringFill but
it appears I can only fill one table at a time not the whole dataset.

Brent...
 
M

Miha Markic [MVP C#]

I looked into DataAdapter.Fill and DataAdapter.AcceptChangesDuringFill but
it appears I can only fill one table at a time not the whole dataset.

You might use a stored procedure that returns multiple tables.
Anyway, filling one table at time shouldn't be a problem if done in correct
order.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top