VC中ADO连接数据库Access失败 5
我用MFC生成了一个基于对话框的程序,在对话框中生成一个CListCtrlm_Note的对象,想测试一下ADO的用法。但数据库连接还是失败了。我在StdAfx.h中加了#...
我用MFC生成了一个基于对话框的程序,在对话框中生成一个
CListCtrl m_Note的对象,想测试一下ADO的用法。但数据库连接还是失败了。我在StdAfx.h中加了#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
并在
BOOL CADOApp::InitInstance()中加了AfxOleInit()函数初始化COM。以下是我的部分代码。请问错在哪里?
m_Note.SetExtendedStyle(LVS_EX_FLATSB
|LVS_EX_FULLROWSELECT
|LVS_EX_HEADERDRAGDROP
|LVS_EX_ONECLICKACTIVATE
|LVS_EX_GRIDLINES);
m_Note.InsertColumn(0,"姓名",LVCFMT_LEFT,70,0);
m_Note.InsertColumn(1,"性别",LVCFMT_LEFT,140,0);
_ConnectionPtr m_pConnection;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\学习练习\\VC练习\\ADO\\test.mdb","","",adModeUnknown);
///连接数据库 ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage; errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);
///显示错误信息
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
int i=0;
while(!m_pRecordset->adoEOF)
{
m_Note.InsertItem(i,"");
m_Note.SetItemText(i,0,(char *)(_bstr_t)m_pRecordset->GetCollect("编号"));
m_Note.SetItemText(i,1,(char *)(_bstr_t)m_pRecordset->GetCollect("姓名"));
m_Note.SetItemText(i,2,(char *)(_bstr_t)m_pRecordset->GetCollect("性别"));
m_pConnection->Close();
m_pRecordset->MoveNext();
i++;
}
m_pRecordset->Close();
m_pConnection->Close(); 展开
CListCtrl m_Note的对象,想测试一下ADO的用法。但数据库连接还是失败了。我在StdAfx.h中加了#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
并在
BOOL CADOApp::InitInstance()中加了AfxOleInit()函数初始化COM。以下是我的部分代码。请问错在哪里?
m_Note.SetExtendedStyle(LVS_EX_FLATSB
|LVS_EX_FULLROWSELECT
|LVS_EX_HEADERDRAGDROP
|LVS_EX_ONECLICKACTIVATE
|LVS_EX_GRIDLINES);
m_Note.InsertColumn(0,"姓名",LVCFMT_LEFT,70,0);
m_Note.InsertColumn(1,"性别",LVCFMT_LEFT,140,0);
_ConnectionPtr m_pConnection;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\学习练习\\VC练习\\ADO\\test.mdb","","",adModeUnknown);
///连接数据库 ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage; errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);
///显示错误信息
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
int i=0;
while(!m_pRecordset->adoEOF)
{
m_Note.InsertItem(i,"");
m_Note.SetItemText(i,0,(char *)(_bstr_t)m_pRecordset->GetCollect("编号"));
m_Note.SetItemText(i,1,(char *)(_bstr_t)m_pRecordset->GetCollect("姓名"));
m_Note.SetItemText(i,2,(char *)(_bstr_t)m_pRecordset->GetCollect("性别"));
m_pConnection->Close();
m_pRecordset->MoveNext();
i++;
}
m_pRecordset->Close();
m_pConnection->Close(); 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询