//以下是本人的花费很力气才做到的,CSDN上没有着方面的,发 // //m_strPassWord m_strPlayerName是之前定义的 //SERVER25 是192.168.1.25上ORACLE数据库服务器的名字 // /// BOOL CNalanElementDlg::OnInitDialog() { CDialog::OnInitDialog(); ................................ // 初始化COM,创建ADO连接等操作 AfxOleInit(); return TRUE; // return TRUE unless you set the focus to a control } void CNalanElementDlg::OnGetPassWord() { CString temp; _bstr_t vUsername,vPassWord; try { HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象 if(SUCCEEDED(hr)) { m_pConnection->Open(L"Provider=OraOLEDB.Oracle.1;User ID=NALAN_OA;Password=NALAN_OA;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.25)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME = SERVER25)));Persist Security Info=False",L"",L"",adOpenUnspecified); } } catch(_com_error e)///捕捉异常 { CString errormessage; errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessag()); AfxMessageBox(errormessage);///显示错误信息 } try { m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open("SELECT * FROM KC_ZH",(IDispatch*) m_pConnection,adOpenStatic,adLockOptimistic,adCmdText); while(!m_pRecordset->adoEOF) { vUsername=m_pRecordset->Fields->GetItem("ACCOUNTS")->Value; temp=vUsername.copy(); if(temp==m_strPlayerName) { vPassWord=m_pRecordset->Fields->GetItem("SNAME")->Value; m_strPassWord=vPassWord.copy(); AfxMessageBox(m_strPassWord); break; } m_pRecordset->MoveNext(); } } catch(_com_error e)///捕捉异常 { CString errormessage; errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息 } m_pRecordset->Close(); //注意!!!不要多次关闭!!!!否则会出错 m_pConnection->Close(); m_pRecordset = NULL; m_pConnection = NULL; }
VC下ADO访问远程ORACLE数据库
最新推荐文章于 2021-02-22 06:36:26 发布