//////////////////////////////////////////// //以下是本人的花費很力氣才做到的,CSDN上沒有著方面的,發(fā) //
//m_strPassWord m_strPlayerName是之前定義的
//SERVER25 是192.168.1.25上ORACLE數(shù)據(jù)庫服務(wù)器的名字
//
///////////////////////////////////////////
BOOL CNalanElementDlg::OnInitDialog()
{
CDialog::OnInitDialog(); ................................ // 初始化COM,創(chuàng)建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");//創(chuàng)建Connection對象
if(SUCCEEDED(hr))
{
m_pConnection->Open("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","","",adOpenUnspecified);
}
}
catch(_com_error e)///捕捉異常
{
CString errormessage;
errormessage.Format("連接數(shù)據(jù)庫失敗!\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("連接數(shù)據(jù)庫失敗!\r\n錯誤信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///顯示錯誤信息
}
m_pRecordset->Close(); //注意!??!不要多次關(guān)閉?。。?!否則會出錯
m_pConnection->Close();
m_pRecordset = NULL;
m_pConnection = NULL;
}
聯(lián)系客服