vc++6.0使用ADO连接数据库SqlServer2000的问题

luyinglply 2010-11-24 04:40:14
还是那个错误

vc++6.0使用ADO连接SqlServer2000数据库 然后把结果放在 CListBox控件里(m_Grid是CListBox的变量)
结果没有反应啊 也不知道哪里出了问题 各位 帮我看看啊
我建了一个对话框的mfc小程序
在app类的BOOL CAODcomApp::InitInstance()里写了

BOOL CAODcomApp::InitInstance()
{
AfxEnableControlContainer();

//--初始化com、ole
BOOL AFXAPI AfxOleInit();

if (!AfxOleInit())
{
AfxMessageBox("ole、com失败");

}
_RecordsetPtr m_pRecordset;
_ConnectionP tr m_pConntion;

// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CAODcomDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();


if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.




HRESULT hr;

hr=m_pConntion.CreateInstance("Connection");

if (!SUCCEEDED(hr))
{
AfxMessageBox("m_pConntion连接失败");

}

//hr=m_pRecordset.CreateInstance(__uuidof(Recordset));
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
if (!SUCCEEDED(hr))
{
AfxMessageBox("m_pConntion连接失败");

}

try
{

m_pConntion->ConnectionString="Provider=SQLOLEDB.1;Password=a1b2c3;User ID=rlsf;Initial Catalog=rlsf;DataSoure=(PC-20100311UUGX)";//本机可以如此表示。

CString strText="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=rlsf;Initial Catalog=rlsf";

m_pConntion->Open((_bstr_t)strText,"","",adConnectUnspecified);


// _variant_t ra;
m_pRecordset=m_pConntion->Execute("select * FROM JCSJ_Czy",NULL,adCmdText);

//while(!m_pRecordset->rsEOF)
for(int i=0;i<=1;i++)
{
//dlg.m_Grid->AddString((_bstr_t)m_pRecordset->GetCollect("dwbm"));

dlg.m_Grid.InsertItem(0,"");
dlg.m_Grid.SetItemText(0,0,(char*)(_bstr_t)m_pRecordset->GetCollect("dwbm"));
//dlg.m_Grid.Update(false);
m_pRecordset->MoveNext();
}



/*m_pRecordset->AddNew();
m_pRecordset->PutCollect("dwbm","08");
m_pRecordset->PutCollect("bm","23");
m_pRecordset->PutCollect("czy","cccc");
m_pRecordset->Update();*/


m_pRecordset->Close();
m_pConntion->Close();
m_pRecordset.Release();
m_pConntion.Release();
CoUninitialize();
}
catch(_com_error e)
{
//AfxMessageBox("failed to read from the database");
CString str = e.ErrorMessage();
TRACE(str);

}



return FALSE;
}

其中//while(!m_pRecordset->rsEOF) 不好用 rsEOF不对 这句话是什么意思啊 我用for循环代替了

但是 一点没有反应 也不报错
...全文
271 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
luyinglply 2010-11-25
  • 打赏
  • 举报
回复
好了 二楼说的对 多谢
马上给分
luyinglply 2010-11-25
  • 打赏
  • 举报
回复
恩 我把代码放到对话框类中了 但是一样没有反应
我现在都不知道数据库 到底有没有连上 有没有取到结果集
owentjr 2010-11-24
  • 打赏
  • 举报
回复
这个问题我也遇到过,问题基本上就是二楼说的
快乐鹦鹉 2010-11-24
  • 打赏
  • 举报
回复
看到这段代码,我有想哭的感觉。
既然数据要写到对话框的listbox控件中,那你这段代码就要写到这个对话框类中。你放在app类算啥啊。
int nResponse = dlg.DoModal();


if (nResponse == IDOK)

这时候对话框都已经OK了,你还怎么玩啊......
Ionstorm2 2010-11-24
  • 打赏
  • 举报
回复
//while(!m_pRecordset->rsEOF) 不好用 rsEOF不对

不是 IsEOF么??

4,012

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧