还少了m_tabledef.Append(),在之前最好先创建字段m_tabledef.CreatField()....,后才m_tabledef.Append().

解决方案 »

  1.   

    vcmfc:
    can U give me a simple but complete sample code?
    Thank U very much!
      

  2.   

    创建库:
    BOOL createDatabase(CDaoDatabase **ppDatabase, CString fileName,
      int dwOptions /* = 0 */)
    {
    // initialize success indicator
    BOOL bSuccess = TRUE; // close and delete if necessary
    if (*ppDatabase != NULL)
    {
    if ((*ppDatabase)->IsOpen())
    closeDatabase(ppDatabase);
    delete *ppDatabase;
    } // construct new database
    *ppDatabase = new CDaoDatabase; // failed to allocate
    if (ppDatabase == NULL)
    return FALSE; // error // now create the database object with error checking
    try
    {
    // default language specified
    (*ppDatabase)->Create(fileName, dbLangGeneral, dwOptions);
    }
    catch (CDaoException *e)
    {
    // create a message to display
    CString message = _T("创建数据库失败,出现异常:");
    message += e->m_pErrorInfo->m_strDescription; // output status
    AfxMessageBox(message); // not rethrowing, so delete exception
    e->Delete(); delete *ppDatabase;
    *ppDatabase = NULL; // failure
    bSuccess = FALSE;
    }
    catch (CMemoryException *e)
    {
    // output status
    AfxMessageBox(_T("Failed to create database--Memory exception thrown.")); // not rethrowing, so delete exception
    e->Delete(); delete *ppDatabase;
    *ppDatabase = NULL; // failure
    bSuccess = FALSE;
    } return bSuccess;
    }创建表:
    BOOL createNewTableDef(CDaoDatabase * pDatabase,
    CDaoTableDef **ppTableDef,
    CString strTableName)
    {
    // if the database is non-existent, then the answer is obvious
    if (pDatabase == NULL)
    return FALSE; // check for existing table with this name just to be safe
    if (IsExistentTable(pDatabase, strTableName))
    {
    AfxMessageBox(_T("A table by that name already exists.")); // return FALSE since can't create duplicate table
    return FALSE;
    } // initialize creation failure indicator
    BOOL bCreateFailed = FALSE; // construct tabledef
    *ppTableDef = new CDaoTableDef(pDatabase); // failed to allocate so exit
    if ((*ppTableDef) == NULL)
    return FALSE; // no duplication, so create the tabledef if possible
    TRY
    {
    (*ppTableDef)->Create(strTableName);
    }
    CATCH (CDaoException, e)
    {
    // construct a meaningful message
    CString strMessage = _T("Couldn't create tabledef--Exception: ");
    strMessage += e->m_pErrorInfo->m_strDescription; AfxMessageBox(strMessage); // indicate failure
    bCreateFailed = TRUE; // delete the tabledef on failure
    if (*ppTableDef != NULL)
    {
    delete *ppTableDef;
    *ppTableDef = NULL;
    }
    }
    AND_CATCH (CMemoryException, e)
    {
    // output status
    AfxMessageBox(_T("Failed to create tabledef--Memory exception thrown.")); // indicate failure
    bCreateFailed = TRUE; // delete the tabledef on failure
    if (*ppTableDef != NULL)
    {
    delete *ppTableDef;
    *ppTableDef = NULL;
    }
    }
    END_CATCH // return TRUE if creation succeeds
    return (!bCreateFailed);
    }创建域:BOOL createNewField(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo)
    {
    // if the tabledef is non-existent, then the answer is obvious
    if (pTableDef == NULL)
    return FALSE; // check for existing field with this name just to be safe
    if (IsExistentField(pTableDef, pFieldInfo->m_strName))
    {
    AfxMessageBox(_T("A field by that name already exists.")); // return FALSE since can't create duplicate field
    return FALSE;
    } // initialize failure indicators
    BOOL bCreateFailed = FALSE; // create a field with the specified properties
    // it is automatically appended to field collection
    // of the tabledef
    TRY
    {
    pTableDef->CreateField(*pFieldInfo);
    }
    CATCH (CDaoException, e)
    {
    // construct a meaningful message
    CString message = _T("Couldn't create field--Exception: ");
    message += e->m_pErrorInfo->m_strDescription; // display message
    AfxMessageBox(message); // indicate failure
    bCreateFailed = TRUE;
    }
    AND_CATCH (CMemoryException, e)
    {
    AfxMessageBox(_T("Failed to create field--Memory exception thrown.")); // indicate failure
    bCreateFailed = TRUE;
    }
    END_CATCH // return TRUE if creation succeeds
    return (!bCreateFailed);
    }打开表:
    BOOL appendTableDef(CDaoDatabase *pDatabase, CDaoTableDef *pTableDef)
    {
    // if the database is non-existent, then the answer is obvious
    if (pDatabase == NULL)
    return FALSE; // initialize success indicator
    BOOL bSuccess = TRUE; // append the tabledef to the collection
    TRY
    {
    pTableDef->Append();
    }
    CATCH (CDaoException, e)
    {
    // construct informative message
    CString strMessage = _T("Couldn't append TableDef--Exception: ");
    strMessage += e->m_pErrorInfo->m_strDescription; // output status
    AfxMessageBox(strMessage); // failure
    bSuccess = FALSE;
    }
    AND_CATCH (CMemoryException, e)
    {
    // output status
    AfxMessageBox(_T("Failed to append tabledef--Memory exception thrown.")); // failure
    bSuccess = FALSE;
    }
    END_CATCH // return status
    return bSuccess;
    }
    ...................................................ok!
      

  3.   

    打开表:
    void OpenClassTable(CDaoDatabase * m_st_data,CDaoRecordset &rs,CString tablename)
    {
    if(!m_st_data->IsOpen())
    openDatabase(&m_st_data,"库名");
    CString strSelect = "Select * From ["+tablename+"]";
    rs.Open(dbOpenDynaset,strSelect);
    if(rs.GetRecordCount()!=0)
    rs.MoveFirst();
    }