回 帖 发 新 帖 刷新版面

主题:求助:如何用C++连接ACCESS数据库

    最近学校要做课程设计,我想用C++连接数据库(ACCESS),请问各位大侠用C++怎么实现啊?

回复列表 (共1个回复)

沙发

貌似以前用VC6做过一个,网上有教程
stdafx.h:
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <icrsint.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
Main.cpp:
#include<stdafx.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void Init_DataBase(HWND);
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR CmdLine,int CmdShow)
{
    HWND hwnd;
    WNDCLASSEX wnd;
    MSG msg;
    char ClassName[]="MyClass";
    wnd.hInstance=hInst;
    wnd.cbSize=sizeof(WNDCLASSEX);
    wnd.lpszClassName=ClassName;
    wnd.lpfnWndProc=WndProc;
    wnd.style=CS_HREDRAW|CS_VREDRAW;
    wnd.cbClsExtra=NULL;
    wnd.cbWndExtra=DLGWINDOWEXTRA;
    wnd.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wnd.hIconSm=wnd.hIcon;
    wnd.hCursor=LoadCursor(NULL,IDC_ARROW);
    wnd.lpszMenuName=NULL;
    wnd.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    if(!RegisterClassEx(&wnd))
    {
        MessageBox(NULL,"ERROR","ERROR",MB_OK);
        return 0;
    }
    hwnd=CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,ClassName,"哈喽啊",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInst,NULL);
    ShowWindow(hwnd,CmdShow);
    UpdateWindow(hwnd);
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
//
char Buffer[1024];
//HWND hwndEdit;
LRESULT CALLBACK WndProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam)
{
    switch(umsg)
    {
    case WM_DESTROY:
        PostQuitMessage(NULL);
        break;
    case WM_CREATE:
        {
    //hwndEdit=CreateWindowEx(NULL,"edit",NULL,WS_CHILD|WS_VISIBLE|ES_MULTILINE,0,0,200,150,hwnd,NULL,((CREATESTRUCT*)lParam)->hInstance,NULL);
    int i=0;
    HDC hdc;
    PAINTSTRUCT ps;
    _ConnectionPtr Conn=NULL;
    _RecordsetPtr rs=NULL;
    CoInitialize(NULL);
    Conn.CreateInstance(__uuidof(Connection));
    Conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);
    rs.CreateInstance(__uuidof(Recordset));
    rs->Open("select * from Table1",Conn.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
    _variant_t Name;
    _variant_t Id;
    rs->MoveFirst();
    while(!rs->adoEOF)
    {
        Id=rs->GetCollect("ID");
        Name=rs->GetCollect("Name");
        MessageBox(NULL,(_bstr_t)Name,(_bstr_t)Id,MB_OK);
        rs->MoveNext();
    }
    rs->Close();
    Conn->Close();
    CoUninitialize();
    break;
        }
    default:
        return DefWindowProc(hwnd,umsg,wParam,lParam);
    }
    return 0;
}
//
void Init_DataBase(HWND hwnd)
{
    return;
}
这个是以前我测试用的写的一个,希望对你有帮助.

我来回复

您尚未登录,请登录后再回复。点此登录或注册