c ++中的数据库访问

时间:2013-11-13 18:29:40

标签: c++

编译此程序时,

#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:\DATABASE\bible.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.0\vc\atlmfc\include\afx.h 24 1 MA_BIBLE */

1 个答案:

答案 0 :(得分:1)

转到项目设置 - &gt;配置属性 - &gt;常规并确保为调试版和发行版设置“在共享DLL中使用MFC”。

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