
ant; ">
void CXIANGXIDLG::OnOK()

{

CoInitialize(NULL);
//初始化COM環(huán)境 
_ConnectionPtr m_pConnection;
//連接對象 
HRESULT hr;
try 
{

hr = m_pConnection.CreateInstance(
"ADODB.Connection");
//創(chuàng )建Connection對象
if(SU
CCEEDED(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 str
SQL;
if (m_name=="")
//路徑1 
{

MessageBox(
"用戶(hù)名不能為空!");
//函數A 
}
else {UpdateData(TRUE);
//函數B
int lenth=0;

lenth=m_name.GetLength();
if (lenth>12 || length<2)
//路徑2 
{MessageBox(
"輸入的用戶(hù)名不正確或沒(méi)有該用戶(hù)!請重新輸入!");
//函數C 
}

Else

{
if(m_pipei)
//路徑3 
{

strSQL=
"SELECT * FROM sj_T_ShouJiKa where 用戶(hù)姓名 = '"+m_name+
"'";
//函數D 
}

Else

{

strSQL=
"SELECT * FROM sj_T_ShouJiKa where 用戶(hù)姓名 like '%"+m_name+
"%'";
//函數E 
}
try 
{

hr=m_pRecordset.CreateInstance(
"ADODB.Recordset");
if(SUCCEEDED(hr))

{
//從數據庫中打開(kāi)表 
m_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);

}
else 
{

AfxMessageBox(
"查詢(xún)不成功!");

}

}
catch (_com_error e)

{

CString strError;

strError.Format(
"警告:打開(kāi)數據表時(shí)發(fā)生異常。 錯誤信息: %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(
"用戶(hù)姓名");

shoujikahao=(_bstr_t)m_pRecordset->GetCollect(
"手機卡號");

tongxinzhishi=(_bstr_t)m_pRecordset->GetCollect(
"通信制式");

fuwushang=(_bstr_t)m_pRecordset->GetCollect(
"服務(wù)商");

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;

}

}
//關(guān)閉連接、釋放com資源m_pRecordset->Close(); //路徑5 
m_pRecordset.Release();

m_pConnection->Close();

m_pConnection.Release();

CoUninitialize();

}

}

}