void CXIANGXIDLG::OnOK() { CoInitialize(NULL);//初始化COM环境 _ConnectionPtr m_pConnection;//连接对象 HRESULT hr; try { hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象 if(SUCCEEDED(hr)) { hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=shouji.mdb","","",adModeUnknown);//连库 } } catch(_com_error e) { AfxMessageBox("数据库连接失败,确认数据库连接字符串是否正确"); } //操纵表 _RecordsetPtr m_pRecordset; //记录集对象 UpdateData(TRUE); CString strSQL; if (m_name=="") //路径1 { MessageBox("用户名不能为空!"); //函数A } else {UpdateData(TRUE); //函数B int lenth=0; lenth=m_name.GetLength(); if (lenth>12 || length<2) //路径2 {MessageBox("输入的用户名不正确或没有该用户!请重新输入!"); //函数C } Else { if(m_pipei) //路径3 { strSQL="SELECT * FROM sj_T_ShouJiKa where 用户姓名 = '"+m_name+"'"; //函数D } Else { strSQL="SELECT * FROM sj_T_ShouJiKa where 用户姓名 like '%"+m_name+"%'";//函数E } try { hr=m_pRecordset.CreateInstance("ADODB.Recordset"); if(SUCCEEDED(hr)) { //从数据库中打开表 m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); } else { AfxMessageBox("查询不成功!"); } } catch (_com_error e) { CString strError; strError.Format("警告:打开数据表时发生异常。 错误信息: %s",e.ErrorMessage()); AfxMessageBox(strError); return; } while(!m_pRecordset->adoEOF) //路径4 { //函数F _bstr_t name=""; _bstr_t shoujikahao=""; _bstr_t tongxinzhishi=""; _bstr_t fuwushang=""; int i=0; m_list5.DeleteAllItems(); while(!m_pRecordset->adoEOF) { name=(_bstr_t)m_pRecordset->GetCollect("用户姓名"); shoujikahao=(_bstr_t)m_pRecordset->GetCollect("手机卡号"); tongxinzhishi=(_bstr_t)m_pRecordset->GetCollect("通信制式"); fuwushang=(_bstr_t)m_pRecordset->GetCollect("服务商"); m_list5.InsertItem(i,name); m_list5.SetItemText(i,1,shoujikahao);//设置该行的不同列的显示字符 m_list5.SetItemText(i,2,tongxinzhishi); m_list5.SetItemText(i,3,fuwushang); m_pRecordset->MoveNext(); i=i+1; } } //关闭连接、释放com资源m_pRecordset->Close(); //路径5 m_pRecordset.Release(); m_pConnection->Close(); m_pConnection.Release(); CoUninitialize(); } } } |