views:

613

answers:

2

Hi guys,

I have table on a database on a server, that is identical to a table on my local. I have click once application that needs to download the version of records on the server down to my local.

At the moment i have webservice that pulls back the records on the server in batches, using asp.net datasets as containers. How do i commit the whole dataset to the table in my local? The table on my local is empty.

Cheers in advance!

A: 

There are several options. Here are the first two that come to mind.

  • One would be to loop through the DataTable and build an SQL Insert statement on each loop and then execute the Insert statement against the local.
  • Another would be to use the SQL Bulk Copy to insert the data
David Stratton
+2  A: 

If you already have a DataSet, containing one or several DataTables, why don't you just use the SqlDataAdapter and call its ".Update()" method with your DataSet?

In the SqlDataAdapter, you can define an InsertCommand, an UpdateCommand, a DeleteCommand which will take care of the three basic insert/update/delete statements for your rows. All you need to do is define / write those three SQL Statements once, and the SqlDataAdapter will do the rest for you (looping through the rows, figuring out whether to insert, update or delete etc.).

If you want, you can even use your basic SELECT statement from the SelectCommand in your DataSet and use the SqlCommandBuilder to build the INSERT, UPDATE and DELETE statements based on your SELECT.

Marc

marc_s
I was using this before. If i use the sqldataadapter, to my understanding, i have to retrieve the data from the table, then run the update method. I will be retrieving alot of records, which i really do not want to do. Since i am doing this insert in batches, initially there wont be any records to pull back, but slowly it gets huge. Maybe there is another way to do it with sqladapters, but i am not sure of this.
pwee167
No, if you have your various SQL statements in place, you don't need to retrieve the data first - you can add it to the DataSet manually, and then call .Update() on the SqlDataAdapter. When you add new rows to the DataSet, they're in a "RowState = added", so they'll be inserted by the appropriate InsertCommand into your database.
marc_s
Thats great. It would be perfect for my problem. Do you have an example of this?
pwee167
Shozld be part of any decent ADO.NET introduction / tutorial. For example, see this CodeProject article: http://www.codeproject.com/KB/database/relationaladonet.aspx
marc_s
Nice, complete answer. +1.
David Stratton