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();

}

}

}