初学ODBC,基本系统看过什么教程,自己实现了一段丑陋的代码,整体构成为首先有一个登陆对话框,对话框类里有成员m_PassSet来连接一个数据库,然后通过对话框数据将游标定位,然后再将m_PassSet传入继承的View类中的*m_pSet里,继续将游标处未读出的数据读出,接下来继续在View中做一系列动作,然后通过响应OnFileSave()函数将动作写回数据库保存.现在的问题是读数据没问题,但只要在响应的OnFileSave()里,m_pSet的m_Database地址就是错误的,难道MFC里有什么位置系统自动关闭了m_pSet与数据库的联接么?这之前我没有加任何的m_pSet->close()函数只要一保存,响应OnFileSave()函数,系统报错0xc0000005:access   violation.希望大家帮帮我......

解决方案 »

  1.   

    加异常处理try
    {
    }
    catch()
    {
    }
      

  2.   

    再顶顶
    然后再稍微把问题详细说一下,就是如果这个OnFileSave()是在InitalUpdate()里响应的,m_pSet就一点儿问题没有,我为了不失去游标,一直没有关闭数据库,在对话框里的m_pass里得到游标,然后传到App里的一个指针*m_SSet里,然后有把这个指针传到View里的m_pSet里,也就是说这想得到数据的话调用这3个应该都能获得同样的结果,现在在View里响应OnFileSave()的话m_pSet里的m_Database就是错误的,但如果在InitalUpdate()里调用OnFileSave()的话m_pSet一点儿问题没有,是不是在InitalUpdate()后框架做了什么工作把m_pSet里的内容改变了?
      

  3.   

    try块也试过了,不好用,在debug里只要执行到跟m_pSet有关的代码,立刻程序就不执行了,比如
    try
    {
      m_pSet->IsOpen();<---debug状态下代码到这就死了......
    }
    catch(CDBException *e)
    {
      AfxMessageBox(e->m_strError);
      e->Delete();
    }
      

  4.   

    建议拿出你的源代码。有没有加add()函数呢,还有UpdateData(TURE)写入数据,UpdateData(FALST)输出数据
      

  5.   

    //StudentPass.h #if !defined(AFX_STUDENTPASS_H__E17FD9C6_ACC4_4DFC_B3B7_386D6AC8316F__INCLUDED_)
    #define AFX_STUDENTPASS_H__E17FD9C6_ACC4_4DFC_B3B7_386D6AC8316F__INCLUDED_#include "CStudentSet.h" // Added by ClassView
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CStudentPass dialogclass CStudentPass : public CDialog
    {
    // Construction
    public:
    BOOL CheckInformation();
    CCStudentSet m_PassSet;
    CStudentPass(CWnd* pParent = NULL);   // standard constructor// Dialog Data
    //{{AFX_DATA(CStudentPass)
    enum { IDD = IDD_CSTUDENT_PASS };
    CString m_TypeInStudentID;
    CString m_TypeInPassword;
    //}}AFX_DATA
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CStudentPass)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected: // Generated message map functions
    //{{AFX_MSG(CStudentPass)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STUDENTPASS_H__E17FD9C6_ACC4_4DFC_B3B7_386D6AC8316F__INCLUDED_)
      

  6.   

    // CStudentView.h : interface of the CCStudentView class
    //
    /////////////////////////////////////////////////////////////////////////////#if !defined(AFX_CSTUDENTVIEW_H__417BEF4E_E98E_4147_839C_46E3AB4206A4__INCLUDED_)
    #define AFX_CSTUDENTVIEW_H__417BEF4E_E98E_4147_839C_46E3AB4206A4__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CCStudentSet;class CCStudentView : public CRecordView
    {
    protected: // create from serialization only
    CCStudentView();
    DECLARE_DYNCREATE(CCStudentView)public:
    //{{AFX_DATA(CCStudentView)
    enum { IDD = IDD_CSTUDENT_FORM };
    CCStudentSet* m_pSet;
    BOOL m_CourseBox1;
    BOOL m_CourseBox2;
    BOOL m_CourseBox3;
    BOOL m_CourseBox5;
    BOOL m_CourseBox4;
    //}}AFX_DATA// Attributes
    public:
    CCStudentDoc* GetDocument();// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CCStudentView)
    public:
    virtual CRecordset* OnGetRecordset();
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    virtual void OnInitialUpdate(); // called first time after construct
    //}}AFX_VIRTUAL// Implementation
    public:
    // BOOL CourseBox[5];
    BOOL CourseCheck();
    // CString m_viewname;
    // CString m_viewcourse;
    virtual ~CCStudentView();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endifprotected:// Generated message map functions
    protected:
    //{{AFX_MSG(CCStudentView)
    // afx_msg void OnFileSave();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };#ifndef _DEBUG  // debug version in CStudentView.cpp
    inline CCStudentDoc* CCStudentView::GetDocument()
       { return (CCStudentDoc*)m_pDocument; }
    #endif///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CSTUDENTVIEW_H__417BEF4E_E98E_4147_839C_46E3AB4206A4__INCLUDED_)