记录集没问题,就是不能在表格控件中显示
//CDataGrid m_ctrlDG;
BOOL ShowGrade::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
// _ConnectionPtr m_ADOCon;
// _RecordsetPtr m_pSet;
m_ADOCon.CreateInstance(__uuidof( Connection));
try
{
m_pSet.CreateInstance (__uuidof(Recordset));
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
_variant_t RecordsAffected;
HRESULT hr;
try
{
hr=m_ADOCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb",
"","",
adModeUnknown);
if (SUCCEEDED(hr))
{
//rs.CursorLocation = adUseClient
m_pSet->CursorLocation=adUseClient;
hr=m_pSet->Open("SELECT * FROM grade",m_ADOCon.GetInterfacePtr(),
// adOpenDynamic,
adOpenStatic,
adLockOptimistic,
adCmdUnknown);
//=m_ADOCon->Execute("",&RecordsAffected,adCmdText);
if(!SUCCEEDED(hr))
AfxMessageBox("Open set error");
}
else
AfxMessageBox("Open database error");
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("记录集创建失败\r\n 错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
////////
//test for m_pSet is succeeded;
// _variant_t var;
// var =m_pSet->GetCollect("序号");
// AfxMessageBox((LPCSTR)_bstr_t(var));
//////
m_ctrlDG.SetCaption(_T("数据库表内容"));
// m_ctrlDG.SetRefDataSource((LPUNKNOWN)m_pSet);
m_ctrlDG.SetRefDataSource(NULL);
m_ctrlDG.SetRefDataSource((LPUNKNOWN)m_pSet);
m_ctrlDG.Refresh();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}