运行到m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";这句时就报错无效指针,下面是源码:
void CEx_ADOView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
try
{
m_pConnection=NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
// CString strCon="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";
m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";
m_pConnection->ConnectionTimeout=30;
HRESULT hr=S_OK;
hr=m_pConnection->Open("","","",adModeUnknown);
//(_bstr_t)strCon
// if (hr!=S_OK)
// {
// AfxMessageBox("Fails!");
// }
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCommand.CreateInstance(__uuidof(Command));
// if (hr!=S_OK)
// {
// ::AfxMessageBox("Link Fails!");
// }
_RecordsetPtr pRstSchema=NULL;
pRstSchema=m_pConnection->OpenSchema(adSchemaColumns);//inquire columns information
CListCtrl& m_ListCtrl=GetListCtrl();
CString strHeader[3]={"序号","TABLE_NAME","COLUMN_NAME"};
for (int i=0;i<3;i++)
{
m_ListCtrl.InsertColumn(i,strHeader[i],LVCFMT_LEFT,120);
}
int nItem=0;
CString str;
_bstr_t value;
while (!(pRstSchema->adoEOF))
{
str.Format("%d",nItem+1);
m_ListCtrl.InsertItem(nItem,str);
for (int i=1;i<3;i++)
{
value=pRstSchema->Fields->GetItem((_bstr_t)(LPCSTR)strHeader[i])->Value;
m_ListCtrl.SetItemText(nItem,i,value);
}
pRstSchema->MoveNext();
nItem++;
}
pRstSchema->Close();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
}