VC使用數(shù)據(jù)庫技巧

字號(hào):

一. 數(shù)據(jù)庫的編程(ADO)
     要用ADO連接數(shù)據(jù)的頭文件中加入 #import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
     // 初始化OLE/COM庫環(huán)境 _ConnectionPtr m_pConnection;
     ::CoInitialize(NULL);
     char buf[256],server[256],datebase[256],uid[256],pwd[256];char str[255];
    GetCurrentDirectory(255,str);//獲得當(dāng)前目錄
    strcat(str,"\\dbinfo.ini");int len0=GetPrivateProfileString("dbinfo","Provider","No Text",buf,256,str);//讀取配置文件中的數(shù)據(jù)庫連接信息
    int len1=GetPrivateProfileString("dbinfo","server","No Text",server,256,str);
    int len2=GetPrivateProfileString("dbinfo","Database","No Text",datebase,256,str);
    int len3=GetPrivateProfileString("dbinfo","uid","No Text",uid,256,str);
    int len4=GetPrivateProfileString("dbinfo","pwd","No Text",pwd,256,str); try
     {
     // 創(chuàng)建Connection對(duì)象
     m_pConnection.CreateInstance("ADODB.Connection");
     // 設(shè)置連接字符串,必須是BSTR型或者_(dá)bstr_t類型
     _bstr_t strConnect = "Provider=";
     strConnect =strConnect+buf;
     strConnect =strConnect+";Server=";
     strConnect =strConnect+server;
     strConnect =strConnect+";Database=";
     strConnect =strConnect+datebase;
     strConnect =strConnect+";uid=";
     strConnect =strConnect+uid;
     strConnect =strConnect+";pwd=";
     strConnect =strConnect+pwd; m_pConnection->Open(strConnect,"","",adModeUnknown);//連接到指定的數(shù)據(jù)庫
     }
     // 捕捉異常
     catch(_com_error e)
     {
     // 顯示錯(cuò)誤信息
     AfxMessageBox(e.Description());
     }
     執(zhí)行無返回值(記錄集)的SQL語句,Insert Update等無返回值的語句
    // Connection對(duì)象的Execute方法:(_bstr_t CommandText, // VARIANT * RecordsAffected, long Options )
     // 其中CommandText是命令字串,通常是SQL命令。
     // 參數(shù)RecordsAffected是操作完成后所影響的行數(shù),
     // 參數(shù)Options表示CommandText的類型:adCmdText-文本命令;adCmdTable-表名
     // adCmdProc-存儲(chǔ)過程;adCmdUnknown-未知
     m_pConnection->Execute(bstrSQL,NULL,adCmdText);
    執(zhí)行有返回值(記錄集)的SQL語句
    _RecordsetPtr m_pRecordset;
    // 創(chuàng)建記錄集對(duì)象
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    // 返回表中的記錄
    m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
    CListCtrl m_list1;//記錄顯示在CListCtrl中
    if(!m_pRecordset->adoEOF) //如果記錄不為空
    {
     while(!m_pRecordset->adoEOF)
     {
     m_list1.InsertItem(i,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID"));
     m_list1.SetItemText(i,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("tintCallType"));
     m_list1.SetItemText(i,2,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("tintHangUp"));
     _variant_t vMaxid(m_pRecordset->GetCollect("strCallingNmb"));
     if(vMaxid.vt==VT_NULL)
     vMaxid="";
     m_list1.SetItemText(i,3,(LPCTSTR)(_bstr_t)vMaxid);
     _variant_t vMaxid0(m_pRecordset->GetCollect("strCalledNmb"));
     if(vMaxid0.vt==VT_NULL)
     vMaxid0="";
     m_list1.SetItemText(i,4,(LPCTSTR)(_bstr_t)vMaxid0);
     _variant_t vMaxid1(m_pRecordset->GetCollect("strOriginNmb"));
     if(vMaxid1.vt==VT_NULL) //此字段在數(shù)據(jù)庫中為NULL值
     vMaxid1=""; i++;//CListCtrl中的第i行顯示此記錄
     m_pRecordset->MoveNext();//下一條記錄
     }
     }
     m_pRecordset->Close();//記錄集讀取完畢,關(guān)閉記錄集
    執(zhí)行帶參數(shù)無返回值(一個(gè)或者多個(gè)記錄集)的存儲(chǔ)過程
    _CommandPtr m_pnewCommand;
    m_pnewCommand.CreateInstance("ADODB.Command"); m_pnewCommand->ActiveConnection=m_pConnection;
    m_pnewCommand->CommandType=adCmdStoredProc;
    m_pnewCommand->CommandText=_bstr_t("proc_deleteSMCBQueue");
    unsigned char newflag = 1;
    _ParameterPtr Pnewflag,PnewrecordID;
    Pnewflag.CreateInstance(__uuidof(Parameter));
    PnewrecordID.CreateInstance(__uuidof(Parameter));
    CString newst="";
    newst.Format("%d",recordID);
    Pnewflag=m_pnewCommand->CreateParameter(_bstr_t("flag"),adTinyInt,adParamInput,1,(_variant_t)newflag);
    m_pnewCommand->Parameters->Append(Pnewflag);
    PnewrecordID=m_pnewCommand->CreateParameter(_bstr_t("recordID"),adInteger,adParamInput,4,(_variant_t)(_bstr_t)newst);
    m_pnewCommand->Parameters->Append(PnewrecordID);
    try
    {
     m_pnewCommand->Execute(NULL,NULL,adCmdStoredProc);
    }
    catch(...)
    {
     }
    執(zhí)行有參數(shù)有返回記錄集的存儲(chǔ)過程(其實(shí)就是執(zhí)行有返回值(記錄集)的SQL語句)
    CString sqlrecord ="exec proc_getSMCBQueue 1";
    try
    {
     m_pRecordset = m_pConnection->Execute((_bstr_t)sqlrecord,NULL,adCmdText);
    }
    catch(...)
    {
    }
    今天就記一下VC關(guān)于數(shù)據(jù)庫的編程吧,下次再記VC關(guān)于網(wǎng)絡(luò)的編程吧!
    只是真正的用_CommandPtr m_pnewCommand執(zhí)行存儲(chǔ)過程返回一個(gè)或者多個(gè)記錄集,再分別對(duì)各記錄集進(jìn)行操作還沒有實(shí)現(xiàn)??!