vc++6.0使用ADO连接数据库SqlServer2000的问题
还是那个错误
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循环代替了
但是 一点没有反应 也不报错