index > .NET Framework Data Access and Storage > Bulk copy data between ODBC sources

Bulk copy data between ODBC sources

Hi,
i want to copy data from one odbc source to another ODBC data table using ado.net
Here the target database may not be MS SQL Server and therefore can't use sqlbulkcopy
The source and target table have the same table definition, but might be on different data sources

Pseudocode:

Imports System.Data.Odbc

Source Table: A
DataTable dtA has the result of 'select * from A' via data adapter daA

Target Table: B
DataAdapter B
DataSet dsB has the result of 'select * from B' via data adapter daB
Target table B is initially empty, so dsB is empty

Now i copied all datarows from dtA to dsB:

Dim dr as new DataRow

For Each dr In dtA.Rows
dsB.Tables(B).ImportRow(dr)
Next

dsB.Tables(B).Rows.Count return a valid positive number equal to the above total imports

However, dsB.HasChanges returns 'False' and the daB doesn't update B with dsB anymore

daB.Update(dsB,B) doesn't update table B, and table B still has zero rows.

Can you tell me where i am going wrong or if there is an alternate way of bulk copying data between two odbc sources? Again, this is NOT specifically for MS SQL Server databases

Thanks,
-srinivas yelamanchili
ysrini

Make sure the code that copies the rows looks like this:

Zlatko Michailov - MSFT

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.
Sarah Parra - MSFT
reply 3

You can use google to search for other answers

 

More Articles

• odbccommand object returning DBNull
• Adding shortcut key to TabControl
• Query a View
• ASP 2.0 NET SQL Provider Membership Management
• Imports cell from DataTable
• Lightweight Transaction Manager SQL Server 2005
• Generating DataSet Relations from XML Schema
• Crystal Report Linked to access database
• Help trying to Insert to Access DB with error No value given for ...
• How to save Access Query Data after user changes?
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Data Adapter Error VB.NET 2003 SQL 2005
• SQLCommand insert not working for datase
• I had the same problem
• ADO.Net Performance
• Should I reinstall?
• Test if a MDB is Corrupted
• XML scheme that causing crashing of the
• How to avoid re-occurring Err "ORA-
• Ado.Net 2.0 Provider compliance?
• HELP!! writing sql in vb.net (ado.net)
• Save data as xml file
• Returning controlled SQL data
• HOT FIX K921883
• My database doesn't update !!!???
• Generating DataSet Relations from XML Sc

Hot Articles

• how to retrieve image from database?
• Way around Application scope connections?
• Bulk copy data between ODBC sources
• Issues with updating Access DataSource
• About Connection and Disconnected Archit
• Advice needed - persist data during a se
• GetChanges - Update - Merge result in re
• DataSet thread safety
• FAQ
• Inserting a row duplicates this row
• boolean to bit when adding a column to a
• Cannot find stored procedure <storedp
• Where to download interop.mapi.dll?
• retrieve schema info for a SQL statement
• Problem | Oracle 9i Release + With Vb.Ne

Recommend Articles

• - Is there a difference between Dataread
• DataSet thread safety
• Unable to cast object of type 'System.Ob
• when a new row is inserted to SQL DATA T
• Program hangs on remote connection when
• Error in executing parameterised query
• What is the easiest way to pass data fro
• Create Table in SQL - VB does not show i
• SQLConnection.GetSchema and ForeignKeys
• System.Data.OleDb.DBBindings.Get_DBTIMES
• Executing Oracle function using ADO.Net
• Merging datasets causes an exception wit
• data objects and their limitations
• Err: Format of the initialization string
• Same Problems