更新資料庫中資訊,更新資料庫資訊

來源:互聯網
上載者:User

更新資料庫中資訊,更新資料庫資訊
作者:BY  www.gudianxiaoshuo.com


本文由 代碼助手軟體 整理髮布 內容與本軟體無關更愜意的讀、更舒心的寫、更輕鬆的發布

 秀色書文軟體  可聽、可讀、可寫、可知識挖掘、可標註,再加上史上最強大的純文字配色功能, 瞬間使您的書文秀色起來。











方式一: 使用SQL語句

更新一列

            strCmd.Format(_T("update classInfo set 班級ID=%d where ID=%d"),                iClassID,                iClassID);            spCmd->CommandText=(LPCTSTR)strCmd;            spCmd->Execute(NULL,NULL,adCmdText);

更新多列 用,號隔開

UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing'WHERE LastName = 'Wilson'

批次更新多項

    try{        CString strCmd;        _CommandPtr spCmd;        spCmd.CreateInstance(__uuidof(Command));        spCmd->ActiveConnection=m_pConnection;        int nCount=m_BanjiList.GetItemCount();        CString ruxuenianStr;        CString banjiStr;        CString xueqiStr;        for (int nIndex=0;nIndex<nCount;nIndex++)        {            ruxuenianStr=m_BanjiList.GetItemText(nIndex,1);            banjiStr=m_BanjiList.GetItemText(nIndex,2);            xueqiStr=m_BanjiList.GetItemText(nIndex,3);            strCmd.Format (                _T("update ClassInfo set 點名=%d where 入學年=%d and 班級=\"%s\" and 學期=\"%s\""),                m_BanjiList.GetItem_bChecked (nIndex),                _ttoi(ruxuenianStr),                banjiStr,                xueqiStr);            spCmd->CommandText=(LPCTSTR)strCmd;            spCmd->Execute(NULL,NULL,adCmdText);        }    }    catch(_com_error &e)    {        AfxMessageBox(e.Error ());    }

    //批次更新班級表中的學號    CString strCmd;    strCmd.Format(        _T("SELECT * FROM 班級表%d order by 學號"),        m_pParentStudentInfo->m_nClassID);    pRecordSet->Open((_variant_t)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);    {        pRecordSet->MoveLast();        int nNew=0;        while (!pRecordSet->adoBOF)        {            varValue=pRecordSet->GetCollect(_T("學號"));            if (varValue.vt!=VT_NULL)            {                nStuXuehao=_ttoi(((LPCTSTR)_bstr_t(varValue)));                //nNew=id_Map[nStuXuehao];                    pRecordSet->PutCollect(_T("學號"),(long)-nStuXuehao);                pRecordSet->MovePrevious();            }                }        pRecordSet->UpdateBatch(adAffectAll);    }

方式二: 使用AddNew Update

        _RecordsetPtr pRecordSet;         CString strCmd;        pRecordSet.CreateInstance(__uuidof(Recordset));        strCmd.Format(            _T("SELECT * FROM 班級表%d where 學號=%d"),    \\先定位要修改的那條記錄            m_pParentStudentInfo->m_nClassID,            nXuehao);                                 pRecordSet->Open((_variant_t)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);    //    pRecordSet->AddNew();  添加新記錄 使用AddNew, 修改記錄,則刪除此語句                pRecordSet->PutCollect(_T("學號"),(long)nXuehao);        pRecordSet->PutCollect(_T("姓名"),(LPCTSTR)nameStr);        pRecordSet->PutCollect(_T("性別"),(LPCTSTR)sexStr);        pRecordSet->PutCollect(_T("地址"),(LPCTSTR)addStr);        pRecordSet->PutCollect(_T("連絡方式"),(LPCTSTR)lianxiStr);        pRecordSet->PutCollect(_T("備忘"),(LPCTSTR)beizhuStr);        CFile f;        CFileException e;        if(f.Open(zhaoPianStr, CFile::modeRead | CFile::typeBinary, &e)) //開啟了一個jpg檔案        {                int nSize = f.GetLength();          //先得到jpg檔案長度            BYTE * pBuffer = new BYTE [nSize];  //按檔案的大小在堆上申請一塊記憶體            if (f.Read(pBuffer, nSize) > 0 )    //把jpg檔案讀到pBuffer(堆上申請一塊記憶體)            {                 BYTE *pBuf = pBuffer;     ///下面這一大段是把pBuffer裡的jpg資料放到庫中                VARIANT            varBLOB;                SAFEARRAY        *psa;                SAFEARRAYBOUND    rgsabound[1];                if(pBuf)                {                        rgsabound[0].lLbound = 0;                    rgsabound[0].cElements = nSize;                    psa = SafeArrayCreate(VT_UI1, 1, rgsabound);                    for (long i = 0; i < (long)nSize; i++)                        SafeArrayPutElement (psa, &i, pBuf++);                    varBLOB.vt = VT_ARRAY | VT_UI1;                    varBLOB.parray = psa;                    pRecordSet->GetFields()->GetItem(_T("照片"))->AppendChunk(varBLOB);                }                delete []pBuffer;                pBuf=NULL;            }            f.Close ();        }        pRecordSet->Update();//更新此記錄        pRecordSet->Close ();        //將此學生更新到課程表中        strCmd.Format(            _T("SELECT * FROM 課堂成績表%d where 學號=%d"),            m_pParentStudentInfo->m_nXueqiID,            nXuehao);        pRecordSet->Open((_variant_t)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);        pRecordSet->PutCollect(_T("學號"),(long)nXuehao);        pRecordSet->PutCollect(_T("姓名"),(LPCTSTR)nameStr);        pRecordSet->Update();            _RecordsetPtr pRecordSet;            CString strCmd;            pRecordSet.CreateInstance(__uuidof(Recordset));            //將此學生更新到課程表中            strCmd.Format(                _T("SELECT * FROM 課堂成績表%d where 學號=%d"),                m_pParentStudentInfo->m_nXueqiID,                nOldXuehao);            pRecordSet->Open((_variant_t)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);            pRecordSet->PutCollect(_T("姓名"),(LPCTSTR)nameStr);            pRecordSet->Update();            pRecordSet->Close ();


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.