To elaborate on Zlatko's response, the reason your original code is not working is because ImportRow preserves the DataRowState of the row you are importing. When you populate a DataTable using DataAdapter.Fill, initially all of the row states are DataRowState.Unchanged. When you copy these values to another DataTable using ImportRow, the new rows will also be Unchanged. When you call DataAdapter.Update, it will only operate on rows that have pending changes. Since these rows are marked as Unchanged, nothing will happen. In your scenario, you want the state to be DataRowState.Added.
If you really need a copy of the data in the app, you can do as Zlatko suggested and use NewRow instead. The DataRowState for the added rows will now be Added. However, another suggestion that still uses the DataTable is to just use one DataTable instead of a second copy, then after filling with data from source A, change the DataRowStates to Added using the SetAdded method. This would be much faster and use less memory than making another copy if you really don't need a copy. Then, for the update, you just use a different DataAdapter that has commands and a connection targeted towards database B. If everything really does have the same schema, you can get away with just one DataTable instead of 2.
Finally, instead of using SetAdded, another option is to use the DataTable.Load method with the LoadOption Upsert. This will avoid having to use SetAdded, because the DataRowStates will be set to Added when the data is loaded in the first place. See the DataTable.Load documentation for more details: http://msdn2.microsoft.com/en-us/library/4e06d41f.aspx
Thanks, Sarah
This posting is provided "AS IS" with no warranties, and confers no rights. |