MFC用ADO打开Access记录集时显示Debug Error怎么解决


我在主窗口CMyDlg里要打开子窗口CStudentDlg,在子窗口的初始化函数里要打开access数据库。代码如下:


 BOOL CStudentDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT
        | LVS_EX_GRIDLINES);

    m_List.InsertColumn(0,"学号",LVCFMT_LEFT,110);
    m_List.InsertColumn(1,"学生姓名",LVCFMT_LEFT,80);
    m_List.InsertColumn(2,"课程",LVCFMT_LEFT,110);
    m_List.InsertColumn(3,"教师姓名",LVCFMT_LEFT,80);
    m_List.InsertColumn(4,"成绩",LVCFMT_LEFT,70);
    m_List.InsertColumn(5,"学分",LVCFMT_LEFT,70);

    AddToList();

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}
void CStudentDlg::AddToList()
{
    CMyDlg dlg;
    //连接数据库
    dlg.OnInitAdoConn();//主窗口已写好
    //设置查询字符串
    BSTR bstrSQL;
    CString str;
    str.Format("SELECT Grade.StudentID, Student.StudentName, Course.CourseName, Teacher.TeacherName, Grade.Grade, Course.Credit FROM Major INNER JOIN (Teacher INNER JOIN ((Class INNER JOIN Student ON Class.ClassID = Student.ClassID) INNER JOIN ((Course INNER JOIN ClassArrange ON Course.CourseID = ClassArrange.CourseID) INNER JOIN Grade ON Course.CourseID = Grade.CourseID) ON Student.StudentID = Grade.StudentID) ON Teacher.TeacherID = ClassArrange.TeacherID) ON Major.MajorID = Class.Major ");
    bstrSQL=str.AllocSysString();  

    //创建记录集指针对象实例
    m_pStudentRecordset.CreateInstance(__uuidof(Recordset));
    //打开记录集
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~问题就在这里~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    m_pStudentRecordset->Open(bstrSQL,m_pStudentRecordset.GetInterfacePtr(),adOpenDynamic,
        adLockOptimistic,adCmdText);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·*/
    while(!m_pStudentRecordset->adoEOF)
    {
        m_List.InsertItem(0,"");
        m_List.SetItemText(0,0,(char*)(_bstr_t)m_pStudentRecordset->GetCollect("学号"));
        m_List.SetItemText(0,1,(char*)(_bstr_t)m_pStudentRecordset->GetCollect("学生姓名"));
        m_List.SetItemText(0,2,(char*)(_bstr_t)m_pStudentRecordset->GetCollect("课程"));
        m_List.SetItemText(0,3,(char*)(_bstr_t)m_pStudentRecordset->GetCollect("教师姓名"));
        m_List.SetItemText(0,4,(char*)(_bstr_t)m_pStudentRecordset->GetCollect("成绩"));
        m_List.SetItemText(0,5,(char*)(_bstr_t)m_pStudentRecordset->GetCollect("学分"));
        //将记录集指针移动到下一条记录
        m_pStudentRecordset->MoveNext();
    }
    //断开数据库连接
    dlg.ExitConnect();//主窗口里已写好

}
点击按钮后就显示Debug Error!请问该如何解决?

vc++ access mfc

工口思念体 9 years, 5 months ago

为什么没有人回答下

Agerter answered 9 years, 5 months ago

Your Answer