微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 CRecordset::GetFieldValue 从 MFC 中的 MS ACCESS 数据库文件中检索记录

如何解决如何使用 CRecordset::GetFieldValue 从 MFC 中的 MS ACCESS 数据库文件中检索记录

拜托,我在使用 getfieldvalue 时遇到问题,如果我在第一个参数上使用单引号,它编译时不会出错,但它不显示数据库内容,在运行时它给了我;数据库错误:字段名称或字段索引不正确,如果我使用双引号,它会编译。


void CehilenDBDlg::OnBnClickedButtonRetrieve(){
    // Todo: Add your control notification handler code here

    CDatabase database;
    CString sqlString;
    CString strID,strName,strAge;
    CString sDriver = L"MICROSOFT ACCESS DRIVER (*.mdb)";
    CString sDsn;
    CString sFile = L"C:\\users\\Admin\\Desktop\\Homebase\\Test.mdb";
    // You must change above path if it's different
    int iRec = 0;
    // Build ODBC connection string
    sDsn.Format(L"ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
 
    // Build ODBC connection string
    TRY{
        // Open the database
        database.Open(NULL,false,sDsn);
        // Allocate the recordset
        CRecordset recset(&database);
        // Build the sql statement
        sqlString = "SELECT ID,Fname,Age " "FROM Employees";
        // Execute the query
        recset.Open(CRecordset::forwardOnly,sqlString,CRecordset::readOnly);
        // Reset List control if there is any data
     
        // populate Grids
        ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLInes);
        // Column width and heading
        m_ListControl.InsertColumn(0,L"Emp ID",LVCFMT_LEFT,-1,0);
        m_ListControl.InsertColumn(1,L"Name",1);
        m_ListControl.InsertColumn(2,L"Age",1);
        m_ListControl.SetColumnWidth(0,120);
        m_ListControl.SetColumnWidth(1,200);
        m_ListControl.SetColumnWidth(2,200);

        // Loop through each record
        while (!recset.ISEOF()) {
            // copy each column into a variable
             // if I use double quotes I get erross
// error C2664: 'void CRecordset::GetFieldValue(short,CStringA &)' : cannot convert argument 1 from 'const char [3]' to 'LPCTSTR'
//13\projects\ehilendb\ehilendbdlg.cpp(260): warning C4309: 'argument' : //truncation of constant value
            recset.GetFieldValue('ID',strID);
            recset.GetFieldValue('name',strName);
            recset.GetFieldValue('Age',strAge);
            // Insert values into the list control
                iRec = m_ListControl.InsertItem(0,strID,0);
            m_ListControl.SetItemText(0,1,strName);
            m_ListControl.SetItemText(0,2,strAge);
            // goto next record
            recset.MoveNext();
        }
        // Close the database
        database.Close();
    }   CATCH(CDBException,e) {
        // If a database exception occured,show error msg
        AfxMessageBox(L"Database error: " + e->m_strError);
    }
    END_CATCH;
}

void CehilenDBDlg::ResetListControl(){

    m_ListControl.DeleteallItems();
    int iNbrofColumns;
    CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgitem(0);
    if (pHeader) {
        iNbrofColumns = pHeader->GetItemCount();
    }

    for (int i = iNbrofColumns; i >= 0; i--) {
        m_ListControl.DeleteColumn(i);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。