您的位置:首页 > 数据库

mfc 链接 access 2007 数据库

2017-06-18 16:03 113 查看

神马也不说了,直接给出源代码和project

原理这个东西 Google 下。都出来了。自己就说下作为新手 ,

1 应该打印出,链接错误原因

2 应该将数据库放到project以下,特别注意这点

给出部分源代码。也能够去看我上传的project

stdafx.h

// stdafx.h : 标准系统包括文件的包括文件,
// 或是常常使用但不常更改的
// 特定于项目的包括文件

#pragma once

#ifndef _SECURE_ATL
#define _SECURE_ATL 1
#endif

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN            // 从 Windows 头中排除极少使用的资料
#endif

#include "targetver.h"

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // 某些 CString 构造函数将是显式的

// 关闭 MFC 对某些常见但常常可放心忽略的警告消息的隐藏
#define _AFX_ALL_WARNINGS

#include <afxwin.h>         // MFC 核心组件和标准组件
#include <afxext.h>         // MFC 扩展

#include <afxdisp.h>        // MFC 自己主动化类

#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h>           // MFC 对 Internet Explorer 4 公共控件的支持
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>             // MFC 对 Windows 公共控件的支持
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <afxcontrolbars.h>     // 功能区和控件条的 MFC 支持

#include <afxdb.h>

#ifdef _UNICODE
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

/*
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")
using namespace ADODB;
*/
#endif
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace \
rename("EOF","adoEOF")rename("BOF","adoBOF")

//using namespace ADODB;

#endif


链接代码.cpp

// mfcLinkAccess.cpp : 定义应用程序的类行为。
//

#include "stdafx.h"
#include "mfcLinkAccess.h"
#include "mfcLinkAccessDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CmfcLinkAccessApp

BEGIN_MESSAGE_MAP(CmfcLinkAccessApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

// CmfcLinkAccessApp 构造

CmfcLinkAccessApp::CmfcLinkAccessApp()
{
// 支持又一次启动管理器
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

// TODO: 在此处加入构造代码,
// 将全部重要的初始化放置在 InitInstance 中
}

// 唯一的一个 CmfcLinkAccessApp 对象

CmfcLinkAccessApp theApp;

// CmfcLinkAccessApp 初始化

BOOL CmfcLinkAccessApp::InitInstance()
{
// 假设一个执行在 Windows XP 上的应用程序清单指定要
// 使用 ComCtl32.dll 版本号 6 或更高版本号来启用可视化方式,
//则须要 InitCommonControlsEx()。否则,将无法创建窗体。

AfxEnableControlContainer();//加入的初始化OLE/COM环境代码
if(!AfxOleInit())
{
AfxMessageBox(_T("初始化OLE DLL失败!"));
return FALSE;
}

HRESULT hr = ::CoInitialize(NULL);
if (!SUCCEEDED(hr))
{
AfxMessageBox(_T("初始换COM失败"));///显示错误信息
}
_ConnectionPtr m_pConnection;
//_bstr_t strConnect(strConnction);
try
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));///创建Connection对象 ;
if (SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=demo.mdb;Persist Security Info=False","","",adModeUnknown);///连接数据库
//上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,    //须要改为:Provider=Microsoft.Jet.OLEDB.3.51;
//AfxMessageBox(_T("链接成功!"));
}
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format(_T("连接数据库失败!\r\n错误信息:%s"), e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
AfxMessageBox(e.Description());///显示错误信息
return -1;
}
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 将它设置为包括全部要在应用程序中使用的
// 公共控件类。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();

AfxEnableControlContainer();

// 创建 shell 管理器,以防对话框包括
// 不论什么 shell 树视图控件或 shell 列表视图控件。
CShellManager *pShellManager = new CShellManager;

// 标准初始化
// 假设未使用这些功能并希望减小
// 终于可执行文件的大小,则应移除下列
// 不须要的特定初始化例程
// 更改用于存储设置的注冊表项
// TODO: 应适当改动该字符串,
// 比如改动为公司或组织名
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

CmfcLinkAccessDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此放置处理何时用
//  “确定”来关闭对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用
//  “取消”来关闭对话框的代码
}

// 删除上面创建的 shell 管理器。
if (pShellManager != NULL)
{
delete pShellManager;
}

// 因为对话框已关闭,所以将返回 FALSE 以便退出应用程序,
//  而不是启动应用程序的消息泵。
return FALSE;
}


还不清楚 就下载我的project吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: