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??/
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? :confused:
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
}
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
}
}
Re: question about dynamically select table with DAO technique!
Quote:
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?
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!
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);
}
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
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".
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!
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
}
Re: question about dynamically select table with DAO technique!
Quote:
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?
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
Re: question about dynamically select table with DAO technique!
:mad:
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?
Re: question about dynamically select table with DAO technique!
i re-execute the dubug, and find when it reach the breakpoint : format, it stops!
Re: question about dynamically select table with DAO technique!
how to post an jpg image here!??