-
March 30th, 2010, 12:01 AM
#1
question about dynamically select table with DAO technique!
an Access database with 100 table in it, all of the tale have same table fields.
now i build a program to handle the database with DAO technique in vc++2008, and dynamically select any table and then add new records into it according to the need and possiblity.
in the program i use the class CDaoDB and CDaoRS inheriting from class of CDaodatabase and CDaorecordset, which is on"http://www.codeproject.com/KB/database/dao.aspx".
how to use only one instance of CDaoRS to dynamically select a table and add new record in it??/
-
March 30th, 2010, 04:19 AM
#2
Re: question about dynamically select table with DAO technique!
If you only want to insert new records in a table - why do you need a CDaoRS class?
Pseudo-code:
Code:
CDaoDB db;
db.dbOpen(_T("MyDB PathName.mdb"));
CString strInsert = _T("Insert INTO Table1 (Field1, Field2, ...) VALUES (Val1, Val2, ...");
try
{
db.Execute(strInsert, dbFailOnError);
}
catch (CDaoException* e)
{
// catch the exception, get/display error code/message
}
Victor Nijegorodov
-
March 30th, 2010, 07:56 AM
#3
Re: question about dynamically select table with DAO technique!
here is a CStingArray instance m_code, which contains string correponding
to the table name in the Access database, and to the binary file name in some
folder. now i use m_code to control the loop which is to add new records to
every table and then in other module of my program to extract records from
any table.
here is the code: could it be done without any error!?
for( int i = 0; i<m_code.GetSize(); i++)
{
CDaoDB db;
db.dbOpen(_T("MyDB PathName.mdb"));
CString strInsert;
strInsert.Format(_T("Insert INTO %s (Field1, Field2, ...) VALUES (Val1, Val2, ...")), m_code.GetAt(i));
try
{
db.Execute(strInsert, dbFailOnError);
}
catch (CDaoException* e)
{
// catch the exception, get/display error code/message
}
}
-
March 30th, 2010, 08:01 AM
#4
Re: question about dynamically select table with DAO technique!
Originally Posted by genliu777
here is the code: could it be done without any error!?
Yes, it could.
Why don't you want to test it?
Victor Nijegorodov
-
March 30th, 2010, 08:15 AM
#5
Re: question about dynamically select table with DAO technique!
because in my program with the code above, every time debugging when it reach the Execute funtion, vc2008 pop up a message box which describes somewhere in memory could not been written or read or something like that!
-
March 30th, 2010, 08:19 AM
#6
Re: question about dynamically select table with DAO technique!
infile.open(_T("600157.day"),std::ios::binary|std::ios::in);
CString tbname = _T("600157");
CDaoDB db;
try
{
db.dbOpen(lpszFile);
long ltemp = 0; // to clear out repeated record
while(!infile.eof())
{
infile.read(reinterpret_cast<char*>(&day), sizeof(Day));
if (day.date==ltemp)
continue;
ltemp = day.date;
CString fvalue;
CString field = (_T("(DATES,OPEN,HIGH,LOW,CLOSE,VOLUME,AMOUNT,DIFF,DEA,MACD,HIT,HIT1,"));
CString field1 = (_T("DIFF_V,DEA_V,MACD_V,HIT_V,HIT_V1,SIGNAL_M,SIGNAL_V)"));
fvalue.Format(_T("(%s,%s,%s,%s,%s,%s,%s,0,0,0,0,0,0,0,0,0,0,0,0)"),day.date,day.open,day.high,day.low,day.close,day.moneysum,day.turnover);
CString strTable;
strTable.Format(_T("INSERT INTO %s %s %s VALUES %s"),600157,field,field1,fvalue);
db.Execute(strTable);
}
infile.close();
db.dbClose();
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription);
}
-
March 30th, 2010, 08:25 AM
#7
Re: question about dynamically select table with DAO technique!
Please, provide the exact error message and error code.
And BTW:
- using Code tags ( http://www.codeguru.com/forum/misc.php?do=bbcode ) makes any code much more readable
- using spaces after commas makes code much more readable
Victor Nijegorodov
-
March 30th, 2010, 08:29 AM
#8
Re: question about dynamically select table with DAO technique!
the messagebox describes the 0x01326a41 memory referenced by "0x7857680d" can not be read. stop the program, please click "OK"; debugg the program ,please click "cancel".
-
March 30th, 2010, 08:30 AM
#9
Re: question about dynamically select table with DAO technique!
sorry for the trouble i make! this the first time i post code here. sorry again!
-
March 30th, 2010, 08:34 AM
#10
Re: question about dynamically select table with DAO technique!
try again:
Code:
DZH5Day day;
ifstream infile;
infile.open(_T("600157.day"),std::ios::binary|std::ios::in);
CString tbname = _T("600157");
CDaoDB db;
try
{
db.dbOpen(lpszFile);
long ltemp = 0; // 用于判断是否有重复记录,数据清洗的一部分!
while(!infile.eof())
{
infile.read(reinterpret_cast<char*>(&day), sizeof(DZH5Day));
if (day.date==ltemp)
continue;
ltemp = day.date;
CString fvalue;
CString field = (_T("(DATES,OPEN,HIGH,LOW,CLOSE,VOLUME,AMOUNT,DIFF,DEA,MACD,HIT,HIT1,"));
CString field1 = (_T("DIFF_V,DEA_V,MACD_V,HIT_V,HIT_V1,SIGNAL_M,SIGNAL_V)"));
fvalue.Format(_T("(%s,%s,%s,%s,%s,%s,%s,0,0,0,0,0,0,0,0,0,0,0,0)"),day.date,day.open,day.high,day.low,day.close,day.moneysum,day.turnover);
CString strTable;
strTable.Format(_T("INSERT INTO 600157 %s %s VALUES %s"),field,field1,fvalue);
db.Execute(strTable,dbFailOnError);
}
infile.close();
db.dbClose();
}
catch (CDaoException* e)
{
// catch the exception, get/display error code/message
}
-
March 30th, 2010, 08:36 AM
#11
Re: question about dynamically select table with DAO technique!
Originally Posted by genliu777
the messagebox describes the 0x01326a41 memory referenced by "0x7857680d" can not be read. stop the program, please click "OK"; debugg the program ,please click "cancel".
So, did you try to debug your program?
What line of your code produces this error?
What do you see in the Call Stack window?
Do the both Format(...) work good and correct?
Victor Nijegorodov
-
March 30th, 2010, 08:47 AM
#12
Re: question about dynamically select table with DAO technique!
when dubugging, it reaches the file "output.c" and points to line 1624. and can not continue
Code:
while (i-- && *pwch) // a yellow pointer direct here
++pwch;
i do not understand
-
March 30th, 2010, 08:52 AM
#13
Re: question about dynamically select table with DAO technique!
Again:
- What line of your code produces this error?
- What do you see in the Call Stack window?
- Do the both Format(...) work good and correct?
Victor Nijegorodov
-
March 30th, 2010, 08:58 AM
#14
Re: question about dynamically select table with DAO technique!
i re-execute the dubug, and find when it reach the breakpoint : format, it stops!
-
March 30th, 2010, 08:59 AM
#15
Re: question about dynamically select table with DAO technique!
how to post an jpg image here!??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|