各位大侠,有没有看过孙鑫老师的VC++视频教程里的第二十课里的数据库编程,他在讲解数据库查询时,用的是列表控件来显示数据,如果用datagrid控件来显示查询数据,该怎么做。
我自己试着做了下,程序编译时没问题,但运行点击查询按钮时,出现如下错误:Runtime Error
This application has requested the Runtime to terminate it in an unusual way,please contact the applation's support team for more information.查询代码如下,请各位大侠赐教。
void CCbfDlg::OnQuery()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(_uuidof(Connection));
_RecordsetPtr pRst(_uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=EquipmentManage";
pConn->Open("","","",-1);
pRst=pConn->Execute("select * from tb_kinds",NULL,adCmdTable);
m_datagrid.SetRefDataSource(NULL);
m_datagrid.SetRefDataSource((LPUNKNOWN)pRst);
m_datagrid.Refresh();
pRst->Close();
pConn->Close();
pRst->Release();
pConn->Release();
CoUninitialize();}
我自己试着做了下,程序编译时没问题,但运行点击查询按钮时,出现如下错误:Runtime Error
This application has requested the Runtime to terminate it in an unusual way,please contact the applation's support team for more information.查询代码如下,请各位大侠赐教。
void CCbfDlg::OnQuery()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(_uuidof(Connection));
_RecordsetPtr pRst(_uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=EquipmentManage";
pConn->Open("","","",-1);
pRst=pConn->Execute("select * from tb_kinds",NULL,adCmdTable);
m_datagrid.SetRefDataSource(NULL);
m_datagrid.SetRefDataSource((LPUNKNOWN)pRst);
m_datagrid.Refresh();
pRst->Close();
pConn->Close();
pRst->Release();
pConn->Release();
CoUninitialize();}
1.如果有值那么就是后半段代码问题,也就是你不理解datagrid如何获取.
2.如果没有值,就是前半段代码问题,说明你连接数据库有问题.