Transferring data from one database to another using DAO MFC classes

There are two ways to copy data from one database to another using DAO MFC classes:

Using CDaoRecordset 

Using CDaoDatabase 

I would prefer CDaoDatabase because it is faster and requires less code to write in compare of CDaoRecordset.

Using CDaoDatabase

You can transfer data from one database to another by using SQL statement. There are two SQL statements that can be used depends on your requirement. One is SELECT..INTO and other is INSERT...INTO.

SELECT...INTO

If your new database is brand new and it doesn't have any tables in it and database schema of both databases are similar then SELECT...INTO statement is prefect for you. SELECT..INTO creates a new table in new database and transfer all data from old table. 

Create an instance of CDaoDatabase and open it with old database.

CDaoDatabase* myDB;

myDB = new CDaoDatabase();

// szOldDBName is database name 

myDB->Open(szOldDBName, FALSE,FALSE,_T(""));

 

Build an SELECT..INTO SQL Statement and call Execute of CDaoDatabase.

Suppose now I want to copy direct oldTable from the old database to newTable in new database which in D:\\ and name is newDatabase.mdb. Here is how you write SQL statement and call Execute method of CDaoDatabase.

sql = "SELECT oldTable.* INTO newTable IN 'D:\\newDatabase.mdb' FROM oldTable ;

myDB->Execute(sql); 

You can even write WHERE clause in this query.

sql = "SELECT oldTable.* INTO newTable IN 'D:\\newDatabase.mdb' FROM oldTable WHERE oldTable.Name = 'Mahesh' ;

myDB->Execute(sql); 

INSERT...INTO

But what if your database schema is different?? Or you already have table in new database? In that case, INSERT..INTO SQL statement would be used. If you dont have a table in the database then you need to create a new table by using CREATE TABLE SQL statement.

Create an instance of CDaoDatabase and open it with old database.

CDaoDatabase* myDB;

myDB = new CDaoDatabase();

if ( !myDB->IsOpen() )

  myDB->Open(szKCDB, FALSE,FALSE,_T("")); 

 

Build an SELECT..INTO SQL Statement and call Execute of CDaoDatabase.

Suppose now I want to copy data from oldTable(DocPath, DocFileName) to newTable(Name, Data) in new database which in D:\\ and name is newDatabase.mdb. Here is how you write SQL statement and call Execute method of CDaoDatabase.

//// Create newTable tables in new database

sql = "CREATE TABLE newTable ( Name VARCHAR(20), Data LONG )" ;

newDB->Execute(sql);

sql = "INSERT INTO newTable (Name, Data) IN 'D:\\newDatabase.mdb' SELECT DocPath, DocFileName FROM oldTable ";

myDB->Execute(sql);

 

 

Using CDaoRecordset

Using CDaoRecordset is pretty easy job. Say you have a table oldTable from old database and a table newTable from new database. Now you want to transfer data from oldTable to newTable.

Now you add a CDaoRecordset derived class from old database and new database each corresponding to the oldTable and the newTable. 

Now open these tables and make a loop until EOF and transfer data from oldTable->members to newTable->members. 

Let's say oldTable has two fields, i.e., Name and Data. newTable has same fields too.

Say your CDaoRecordset derived classes are COldTableSet and CNewTableSet corresponding to oldTable and newTable and these recordsets has data members m_Name and m_Data for each field. Now here are the steps:

// Create Instances  

COldTableSet* oldRec;

CNewTableSet* newRec;

if ( !oldRec->IsOpen() )

  oldRec->Open();

if ( !newRec->IsOpen() )

  newRec->Open();

// See if oldRec is not empty

if ( oldRec->IsBOF() && oldRec->IsEOF() )

return;

if ( ! oldRec->IsEOF() )

  oldRec->MoveFirst();

// Make loop until oldTable EOF

while (! OldRec->IsEOF() )

{

  newRec->AddNew();

  newRec->m_Name = oldRec->m_Name;

  newRec->m_Data = oldRec->m_Data;

  oldRec->MoveNext();

}

newRec->Update();