下面是  jiaguiyun (龙翔九天)   发的帖子
做一个数据库,采用ADO,动态联接和产生纪录集后,尽管设置了数据源并调用了CDataGrid::refresh(),可是还是不能显示纪录。下面是测试用的代码,请大家帮忙看看。
编译通过,可以正常运行,纪录集也确实正常产生。void CBoreholeView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
ResizeParentToFit();
    HRESULT hr;
    _ConnectionPtr m_pConnection;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=stdreg.mdb","","",adModeUnknown);///连接数据库
}
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
// return FALSE;

CString strTableName("Course");
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(_variant_t("SELECT * FROM Course"),_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
    
      TRACE("\n%d\n",m_pRecordset->GetRecordCount());
_variant_t vID,vTitle,vHours;
while(!m_pRecordset->adoEOF)
{
vID=m_pRecordset->GetCollect(_variant_t("CourseID"));
vTitle=m_pRecordset->GetCollect(_variant_t("CourseTitle"));
vHours=m_pRecordset->GetCollect(_variant_t("Hours"));
TRACE("%s    ",(LPCTSTR)(_bstr_t)vID);
TRACE("%s    ",(LPCTSTR)(_bstr_t)vTitle);
        TRACE("%d\n",(INT)VHours);
m_pRecordset->MoveNext();
}
    m_dataGrid.SetCaption("");
    m_dataGrid.SetRefDataSource(NULL);
    m_dataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
    m_dataGrid.Refresh();
    UpdateData(FALSE);
}
我也用这个方法做了,可以象  jiaguiyun (龙翔九天)  说的一样,不能显示

解决方案 »

  1.   

    再看一个老外写的,这个就可以,不过他一开始要你选择数据源(难道区别就在这里吗?)
    CMainFrame* pMainFrame = reinterpret_cast<CMainFrame*>(AfxGetMainWnd());
        if (pMainFrame)
        {
            m_pRS = NULL;
            m_pRS.CreateInstance( __uuidof(Recordset));         try 
        {     
    m_pRS->CursorLocation = adUseClient;
                m_pRS->Open((LPCSTR)sTableName, (LPCSTR)pMainFrame->m_ptrConnection->GetConnectionString(), adOpenKeyset,adLockOptimistic, adCmdTable);
        }
        catch (_com_error &e)
        {
        AfxMessageBox(GetErrorDescription(e));
        }
        
        //Demonstrates, how to populate DataGrid by assigning it a Recordset object.
            m_ctlDataGrid.SetCaption(sTableName);
        m_ctlDataGrid.SetRefDataSource(NULL);
        m_ctlDataGrid.SetRefDataSource( (LPUNKNOWN) m_pRS );
        m_ctlDataGrid.Refresh();     
        UpdateData(FALSE);
      

  2.   

    不可能没有人知道吧!我记的有人做出来的,一起来讨论一下呀?
    MSN:[email protected]
      

  3.   

    m_pConnection->Open()
    是不是open后面结口的问题呢?
      

  4.   

    对于ADO数据库我以前用代码实现过,不过常用DAO数据库。
    与DataGrid连用还是第一次。容我思考。
      

  5.   

    楼上的两位在吗?
    做出来了吗?为什么没有人来讨论呢?
    难道没有人知道?
    不可能吧?
    郁闷ing
      

  6.   

    呵呵,给你发过代码了
    其实很好用啊,只要记录集不是用adOpenForwardOnly参数打开的
    应该不会有什么问题
    当然在一些复杂的情况,如与连接分离或者说孤立的记录集
    在删除记录以后,要注意将记录集的游标移动到适当的位置等情况
      

  7.   

    我也碰到相同问题,请大虾出手。
    [email protected]
      

  8.   

    解决了:)这一句是关键!
    m_pRS->CursorLocation = adUseClient;参见:(from MSDN ---> CursorLocation Property - ADO)Sets or returns the location of the cursor engine. Applies To Connection, Recordset Settings And Return Values Sets or returns a Long value that can be set to one of the following constants: 
    Constant Description 
     
    adUseClient Uses client-side cursors supplied by a local cursor library. Local cursor engines will often allow many features that driver-supplied cursors may not, so using this setting may provide an advantage with respect to features that will be enabled. For backward-compatibility, the synonym adUseClientBatch is also supported. 
    adUseServer Default. Uses data provider or driver-supplied cursors. These cursors are sometimes very flexible and allow for some additional sensitivity to reflecting changes that others make to the actual data source. However, some features of the Microsoft Client Cursor Provider (such as disassociated recordsets) cannot be simulated with server-side cursors and these features will then become unavailable with this setting.