C++ 中的数据库访问

Database access in c++

本文关键字:访问 数据库 C++      更新时间:2023-10-16

当我编译这个程序时,

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <afxdb.h>
#define _AFXDLL
int main(int argc, char *argv[])
{
    CString sDsn;
    CDatabase db;
    sDsn = "ODBC; DRIVER={MICROSOFT ACCESS DRIVER (*.mdb)}; DSN = ''; DBQ = D:DATABASEbible.mdb";
    db.Open(NULL, false, false, sDsn);
    try
    {
        CRecordset rs(&db);
        CString strOut;
        CString strOut2;
        CString strOut3;
        CString SqlString = "SELECT nom_prophete FROM L_PROPHETE";
        rs.Open(CRecordset::snapshot, SqlString, CRecordset::appendOnly);
        rs.GetFieldValue((short)0, strOut);
        AfxMessageBox(strOut);
        // --- petite transformation en string
        // --- pour l'affichage sur la console
        CT2CA pszConvertedAnsiString(strOut);
        std::string strStd(pszConvertedAnsiString);
        std::cout << "--" << strStd.c_str() << std::endl;
        rs.MoveNext();
        // --- petite transformation en string
        // --- pour l'affichage sur la console
        CT2CA pszConvertedAnsiString2(strOut2);
        std::string strStd2(pszConvertedAnsiString2);
        std::cout << "--" << strStd2.c_str() << std::endl;
        rs.MoveNext();
        rs.GetFieldValue((short)0, strOut3);
        AfxMessageBox(strOut3);
        // -- petite transformation en string
        // -- pour l'affichage sur la console
        CT2CA pszConvertedAnsiString3(strOut3);
        std::string strStd3(pszConvertedAnsiString3);
        std::cout << "--" << strStd3.c_str() << std::endl;
        rs.Close();
    }
    catch (CDBException *p)
    {
        AfxMessageBox(p->m_strError);
    }
    return 0;
}

它显示此错误,

Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] c:program files (x86)microsoft visual studio 12.0vcatlmfcincludeafx.h 24 1 MA_BIBLE */

转到"

项目设置"-">配置属性"->"常规",并确保为调试版本和发布版本设置"在共享 DLL 中使用 MFC"。

此外,如果您在某处包含 windows.h - 删除该行,因为 MFC 项目应仅包含 afxwin.h