下面是 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 (龙翔九天) 说的一样,不能显示
做一个数据库,采用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 (龙翔九天) 说的一样,不能显示
解决方案 »
- 关于CTime t = CTime::GetCurrentTime()
- 关于网络字节序与主机字节序的问题
- 关于sleeping 进程
- 关于CTimeSpan使用问题,请求各位支招
- 请教,关闭 CAsynSocket 对象问题,在线等
- 如何让vc的类与回调函数共享一个变量,请多指点
- 怎么改变一个对话框中的按钮,编辑框的位置??
- TMD,再开50分,关于 unsigned short * 赋值的问题
- MFC中如何调用ATL的COM库和.net库
- html中两个fieldset为什么设置了等高,但是根据实际的内容显示后,不等高
- 大家中秋快乐呀!工作的同时可不要忘了吃月饼哟~~呵呵
- 网上有什么项目是由网友共同开发的呀(VC或C++的都可以)?我也想接一部分来做,锻炼锻炼嘛!
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);
MSN:[email protected]
是不是open后面结口的问题呢?
与DataGrid连用还是第一次。容我思考。
做出来了吗?为什么没有人来讨论呢?
难道没有人知道?
不可能吧?
郁闷ing
其实很好用啊,只要记录集不是用adOpenForwardOnly参数打开的
应该不会有什么问题
当然在一些复杂的情况,如与连接分离或者说孤立的记录集
在删除记录以后,要注意将记录集的游标移动到适当的位置等情况
[email protected]
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.