Transferring data from one database to another using MFC ODBC classes
Submitted by Date of submission User level
Mahesh Chand Nov 30, 2000 Beginner
There are two ways to copy data from one database to another using ODBC MFC classes:
Using CRecordset
Using CDatabase
Both methods are pretty easy to use.
Using CDatabase
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 the old table.
Create an instance of CDatabase and open it with old database.
CDatabase* myDB;
myDB = new CDatabase();
// "mhDSN" is your data source name here. See ODBC Admin article for more details.
myDB->Open("mhDSN", FALSE, FALSE, _T("ODBC;UID=Admin"), FALSE);
Build an SELECT..INTO SQL Statement and call Execute of CDatabase.
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 COdbcDatabase.
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 don't have a table in the database then you need to create a new table by using CREATE TABLE SQL statement.
Create an instance of CDatabase and open it with a data source
CDatabase* myDB;
myDB = new CDatabase();
if ( !myDB->IsOpen() )
myDB->Open("mhDSN", FALSE, FALSE, _T("ODBC;UID=Admin"), FALSE);
Build an SELECT..INTO SQL Statement and call Execute of COdbcDatabase.
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 CDatabase.
//// 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 CRecordset
Using CRecordset 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.
You add a CRecordset 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 CRecordset 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();
Did you like or dislike this article? Please send your feedback to the Author. Your feedback helps us to improve the quality of our next article.
About the Author:
Mahesh is Admin and the founder of this site. He has been programming in VC++, Visual Basic, COM, ATL, Database Programming for 4 years. He can be reached at Mahesh. His background includes Master's in Computer Science and Applications and Batchelor's in Mathematics and Physics