在classwizard中创建一个继承于CRecordSet类,并连接好了数据库,这个类跟书本的给出的例子一模一样,但却出现了错误,就是:'CRecordset' : base class undefined,请问怎么会这样,怎样才能解决?

解决方案 »

  1.   

    在格stdafx.h
    包含了#include <afxdb.h>没有?
      

  2.   

    .h文件:
    #if !defined(AFX_ADDCATEGDLG_H__2F818918_4AB0_461D_BEC6_EC71B561B32B__INCLUDED_)
    #define AFX_ADDCATEGDLG_H__2F818918_4AB0_461D_BEC6_EC71B561B32B__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // AddCategDlg.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CAddCategDlg dialogclass CAddCategDlg : public CDialog
    {
    // Construction
    public:
    CAddCategDlg(CWnd* pParent = NULL);   // standard constructor// Dialog Data
    //{{AFX_DATA(CAddCategDlg)
    enum { IDD = IDD_ADD_CATEG_DLG };
    CString m_strName;
    CString m_strNote;
    //}}AFX_DATA
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAddCategDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected: // Generated message map functions
    //{{AFX_MSG(CAddCategDlg)
    virtual void OnOK();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()private:
    int WriteData();
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_ADDCATEGDLG_H__2F818918_4AB0_461D_BEC6_EC71B561B32B__INCLUDED_)
    .cpp文件:
    // AddCategDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "MyMFCODBCSmpl.h"
    #include "AddCategDlg.h"
    #include "MainFrm.h"
    #include "CategorySet.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAddCategDlg dialog
    CAddCategDlg::CAddCategDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CAddCategDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CAddCategDlg)
    m_strName = _T("");
    m_strNote = _T("");
    //}}AFX_DATA_INIT
    }
    void CAddCategDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAddCategDlg)
    DDX_Text(pDX, IDC_CATEG_NAME, m_strName);
    DDX_Text(pDX, IDC_CATEG_NOTE, m_strNote);
    //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CAddCategDlg, CDialog)
    //{{AFX_MSG_MAP(CAddCategDlg)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CAddCategDlg message handlersvoid CAddCategDlg::OnOK() 
    {
    // TODO: Add extra validation here

    CDialog::OnOK();
    }int CAddCategDlg::WriteData()
    {
        int n Ret=-1;
    if(m_strName.IsEmpty())
    {
    AfxMessageBox("产品分类名称不能为空!");
    return nRet;
    }
    CDatabase db;
    if(!db.Open(_T("MyNWind")))
    {
    AfxMessageBox("数据源连接失败!");
    return nRet;
    }
    if(!db.BeginTrans())
    return nRet;
    TRY
    {
    CString strSql="Insert into Categories(CategoryName,Description) values(";
    CString strTemp;
    strTemp=m_strName;
    strSql+="'";
    strSql+=strTemp;
    strSql+="'";
    strSql+=",";
    strSql+="'";
    strTemp=m_strNote;
    strSql+="'";
    strSql+=")";
    db.ExecuteSQL(strSql);
    db.CommitTrans();
    }
    CATCH(CDBException,e)
    {
    db.Rollback();
    AfxMessageBox(e->m_strError);
    e->Delete();
    return nRet;
    }
    END_CATCH;
    CCategorySet *pSet=new CCategorySet(&db);
    if(!pSet->IsOpen())pSet->Open();
    pSet->m_strSort="ASC CategoryID";
    pSet->MoveLast();
    nRet=pSet->m_CategoryID;
    pSet->Close();
    delete pSet;
    db.Close();
    return nRet;
    }
      

  3.   

    把CategorySet.h和CategorySet.cpp贴出来
      

  4.   

    #if !defined(AFX_CATEGORYSET_H__CFCF419B_161E_45B8_BDBC_367EA1A87A85__INCLUDED_)
    #define AFX_CATEGORYSET_H__CFCF419B_161E_45B8_BDBC_367EA1A87A85__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // CategorySet.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CCategorySet recordsetclass CCategorySet : public CRecordset
    {
    public:
    CCategorySet(CDatabase* pDatabase = NULL);
    DECLARE_DYNAMIC(CCategorySet)// Field/Param Data
    //{{AFX_FIELD(CCategorySet, CRecordset)
    long m_CategoryID;
    CString m_CategoryName;
    CString m_Description;
    CLongBinary m_Picture;
    //}}AFX_FIELD
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CCategorySet)
    public:
    virtual CString GetDefaultConnect();    // Default connection string
    virtual CString GetDefaultSQL();    // Default SQL for Recordset
    virtual void DoFieldExchange(CFieldExchange* pFX);  // RFX support
    //}}AFX_VIRTUAL// Implementation
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    .h文件
    #endif
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CATEGORYSET_H__CFCF419B_161E_45B8_BDBC_367EA1A87A85__INCLUDED_)
    .cpp文件:
    // CategorySet.cpp : implementation file
    //#include "stdafx.h"
    #include "MyMFCODBCSmpl.h"
    #include "CategorySet.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CCategorySetIMPLEMENT_DYNAMIC(CCategorySet, CRecordset)CCategorySet::CCategorySet(CDatabase* pdb)
    : CRecordset(pdb)
    {
    //{{AFX_FIELD_INIT(CCategorySet)
    m_CategoryID = 0;
    m_CategoryName = _T("");
    m_Description = _T("");
    m_nFields = 4;
    //}}AFX_FIELD_INIT
    m_nDefaultType = snapshot;
    }
    CString CCategorySet::GetDefaultConnect()
    {
    return _T("ODBC;DSN=MyNWind");
    }CString CCategorySet::GetDefaultSQL()
    {
    return _T("[dbo].[Categories]");
    }void CCategorySet::DoFieldExchange(CFieldExchange* pFX)
    {
    //{{AFX_FIELD_MAP(CCategorySet)
    pFX->SetFieldType(CFieldExchange::outputColumn);
    RFX_Long(pFX, _T("[CategoryID]"), m_CategoryID);
    RFX_Text(pFX, _T("[CategoryName]"), m_CategoryName);
    RFX_Text(pFX, _T("[Description]"), m_Description);
    RFX_LongBinary(pFX, _T("[Picture]"), m_Picture);
    //}}AFX_FIELD_MAP
    }/////////////////////////////////////////////////////////////////////////////
    // CCategorySet diagnostics#ifdef _DEBUG
    void CCategorySet::AssertValid() const
    {
    CRecordset::AssertValid();
    }void CCategorySet::Dump(CDumpContext& dc) const
    {
    CRecordset::Dump(dc);
    }
    #endif //_DEBUG
      

  5.   

    错误,就是:'CRecordset' : base class undefined
      

  6.   

    用点心看一下,应该是CRecrodSet啊
      

  7.   

    class CCategorySet : public CRecordset
      

  8.   

    CRecordset
    是没有错的,将程序发给我吧
    [email protected]
      

  9.   

    可以的话,也给一份代码给我好吗?[email protected]