7,713
社区成员
发帖
与我相关
我的任务
分享
// 32位win7+32位office2010下
//vs2010 visual C++用ADO连接acces数据库代码!(win32控制台应用程序)
//如果您要调试下,请把附件的access文档放在D盘下
#include "stdafx.h"
#include <afxdb.h> // AfxMessageBox命令需要
#import "C:\Program Files\Common Files\System\ado\msado15.dll"\
no_namespace rename("EOF","adoEOF")
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL); //初始化COM对象 另一种初始化AfxOleInit()会报错?
_ConnectionPtr m_pConnection; //定义_ConnectionPtr类型指针
//**********连接Connection对象
try
{
m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
m_pConnection->Open("Microsoft.ACE.OLEDB.12.0;\
Data Source=D:\test.accdb","","",adModeUnknown);///连接数据库。也可以试试下面的
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(_T("连接数据库失败! 错误信息:%s"),e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
//**********关闭Connection对象
if(m_pConnection->State)
{
m_pConnection->Close();
}
CoUninitialize(); // 卸载COM库
return 0;
}
#include <afxdb.h>// AfxMessageBox命令需要
#import "C:\Program Files\Common Files\System\ado\msado15.dll"\
no_namespace rename("EOF","adoEOF")
m_pConnection->Open("Microsoft.ACE.OLEDB.12.0;\
应该改成m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;\
坑爹的微软,这么简单的错误就不能提示下语法错误!貌似vs是对C++没有VB和V#优化的好,毕竟不是亲生的catch(_com_error e)///捕捉异常
{
CString errormessage;
//errormessage.Format(_T("连接数据库失败! 错误信息:%s"),e.ErrorMessage());
errormessage.Format(e.Description(), e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}