mfc用ado的方式连接access

1、包含头文件,导入dll

// ado

#include <atlbase.h>
#import "MSADOX.dll"
#import "MSADO15.dll"  no_namespace rename("EOF", "adoEOF")
#define TESTHR(x) if FAILED(x) _com_issue_error(x);
#define TEXT_CHANGED _T("Changed")
#define TEXT_MARKED _T("Marked")

#define TEXT_SCANNER _T("Scanner")


2、初始化

bool CxinjinDlg::InitConn(CString strFileNeme)
{
::CoInitialize(NULL);
CTime __tCurrent = CTime::GetCurrentTime();
/*g_strWeek = __tCurrent.Format("%Y_%U");*/
g_strWeek = __tCurrent.Format("%Y");
if (!IsExistFile(strFileNeme, _T("mdb"))) // 如果数据库不存在则需要打开连接
{
if (!CreateMdb(GetAbsolutePathName(strFileNeme))) return false;
}
HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
AfxMessageBox(_T("_ConnectionPtr對象指針實例化失敗"));
return false;
}
try
{
CString strConnect = _T("");
strConnect.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='%s'"), GetAbsolutePathName(strFileNeme));
m_pConnection->Open(strConnect.GetBuffer(),"","",adModeUnknown);
}
catch(_com_error &e)
{
AfxMessageBox(e.Description());
// if (CreateMdb(databasepath)) return InitConn(databasepath);
return false;
}
m_pRecordSet.CreateInstance(_uuidof(Recordset));


if (!IsTalbeExit()) 
return CreateTable();
return true;
}



3、IsExistFile判断mdb文件是否存在

BOOL CxinjinDlg::IsExistFile(CString strFileName, CString strType, CString strFolder)
{
CString strPath = strFolder==_T("")?GetAbsolutePathName(_T("")):strFolder, str2 = _T("");
strPath += _T("\\*.");
strPath += strType==_T("")?_T("*"):strType;
CFileFind finder;
BOOL bRet = finder.FindFile(strPath);
if (!bRet) return FALSE;
if (strType==_T(""))
str2.Format(_T("%s"), strFileName);
else
{
if (strFileName.Find('.')) str2 = strFileName;
else str2.Format(_T("%s.%s"), strFileName,strType);
}
while (bRet)
{

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值