如何使用 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 (将#修改为@)