CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Mar 2010
    Posts
    22

    Question 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??/

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    Join Date
    Mar 2010
    Posts
    22

    Question 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
    }
    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: question about dynamically select table with DAO technique!

    Quote Originally Posted by genliu777 View Post
    here is the code: could it be done without any error!?
    Yes, it could.
    Why don't you want to test it?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2010
    Posts
    22

    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!

  6. #6
    Join Date
    Mar 2010
    Posts
    22

    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("(&#37;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);
    }

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: question about dynamically select table with DAO technique!

    Please, provide the exact error message and error code.

    And BTW:
    1. using Code tags ( http://www.codeguru.com/forum/misc.php?do=bbcode ) makes any code much more readable
    2. using spaces after commas makes code much more readable
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2010
    Posts
    22

    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".

  9. #9
    Join Date
    Mar 2010
    Posts
    22

    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!

  10. #10
    Join Date
    Mar 2010
    Posts
    22

    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("(&#37;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
    	}

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: question about dynamically select table with DAO technique!

    Quote Originally Posted by genliu777 View Post
    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

  12. #12
    Join Date
    Mar 2010
    Posts
    22

    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

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: question about dynamically select table with DAO technique!


    Again:
    1. What line of your code produces this error?
    2. What do you see in the Call Stack window?
    3. Do the both Format(...) work good and correct?
    Victor Nijegorodov

  14. #14
    Join Date
    Mar 2010
    Posts
    22

    Re: question about dynamically select table with DAO technique!

    i re-execute the dubug, and find when it reach the breakpoint : format, it stops!

  15. #15
    Join Date
    Mar 2010
    Posts
    22

    Re: question about dynamically select table with DAO technique!

    how to post an jpg image here!??

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured