在MyAdoDB.cpp中添加以下 代码 我全部复制了
// MyAdoDB.cpp: implementation of the CMyAdoDB class.
//
//
#include "stdafx.h"
#include "MyAdoDB.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
CMyAdoDB::CMyAdoDB()
{
AfxOleInit();//初始化COM库
AfxEnableControlContainer();
}
CMyAdoDB::~CMyAdoDB()
{
}
----打开数据文件连接
BOOL CMyAdoDB::Connect(CString strConnStr)
{
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
_bstr_t strConnect = _bstr_t(strConnStr);
m_pConnection->ConnectionTimeout=30;///设置超时时间为30秒
//连接数据库
hr = m_pConnection->Open(strConnect,"","",adModeUnknown);
return TRUE;
}
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("错误信息:%s",e.ErrorMessage());
MessageBox(NULL,temp, "失败", MB_OK);
return FALSE;
}
return FALSE;
}
----关闭数据库连接
BOOL CMyAdoDB::CloseConnection()
{
//以下是连接access2000数据库。。。
try
{
if(m_pConnection->State)
return m_pConnection->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("错误信息:%s",e.ErrorMessage());
MessageBox(NULL, temp, "失败", MB_OK);
return FALSE;
}
return FALSE;
}