1、拷贝msado15.dll到工程文件中。
2、在stdafx.h添加 #import "F:\资料\数据库设计\MangeAccess\msado15.dll" no_namespace rename("EOF","adoEOF")
3、在BOOL CMangeAccessApp::InitInstance()添加
if (!AFXOLEAPI())
{
AfxMessageBox("初始化OLE DLL失败!");
return FALSE;
}
4,添加变量 : _ConnectionPtr m_pConnection;
5、在onInitDialog()添加:
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance("ADODB.Connection");
if (SUCCEEDED(hr))
hr=m_pConnection->Open("Provider=Microsof.Jet.OLEDB.4.0;Data Source=my.mdb","","",adModeUnknown);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
6、增加按钮控件
void CMangeAccessDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_variant_t RecordsAffected;
UpdateData(TRUE);
if (m_strAccess.IsEmpty())
{
AfxMessageBox("数据不能为空");
return;
}
CString strSQL;
strSQL.Format("insert into student(t_name) values('%s')",m_strAccess);
m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
AfxMessageBox(m_strAccess);
m_strAccess="";
UpdateData(FALSE);
}