往資料庫中添加照片,資料庫添加照片

來源:互聯網
上載者:User

往資料庫中添加照片,資料庫添加照片

作者:BY  www.gudianxiaoshuo.com


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

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









往資料庫中添加照片

        pRecordSet.CreateInstance(__uuidof(Recordset));        strCmd.Format(            _T("SELECT * FROM 班級表%d order by 學號 "),            m_pParentStudentInfo->m_nClassID);        pRecordSet->Open((_variant_t)strCmd,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);            pRecordSet->AddNew();        pRecordSet->PutCollect(_T("學號"),(long)nXuehao);        pRecordSet->PutCollect(_T("姓名"),(LPCTSTR)nameStr);        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();

讀取照片

   CString    xuehaoStr=m_XueShengList.GetItemText(nIndex,0); //選中行的第一列的文字    _CommandPtr spCmd;    _RecordsetPtr pRecordSet;    spCmd.CreateInstance(__uuidof(Command));    spCmd->ActiveConnection=m_pConnection;    CString strCmd;    strCmd.Format(        _T("select * from  班級表%d where 學號=%d"),        m_pParentStudentInfo->m_nClassID,        _ttoi(xuehaoStr));    spCmd->CommandText=(LPCTSTR)strCmd;    pRecordSet=spCmd->Execute (NULL,NULL,adCmdText);    if (pRecordSet->adoEOF) //未找到此學號的學生        return;    if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData();                long nSize=pRecordSet->GetFields()->GetItem(_T("照片"))->ActualSize;    if(nSize>0)    {        _variant_t varBLOB;        varBLOB=pRecordSet->GetFields()->GetItem(_T("照片"))->GetChunk(nSize);                if (varBLOB.vt==(VT_ARRAY|VT_UI1))        {            if (BYTE* pBuffer=new BYTE[nSize+1])            {                char *pBuf=NULL;                SafeArrayAccessData(varBLOB.parray,(void**)&pBuf);                memcpy(pBuffer,pBuf,nSize);                SafeArrayUnaccessData(varBLOB.parray);                m_Pic.LoadPictureData(pBuffer,nSize);                delete [] pBuffer;                pBuf=NULL;                        //PICTURE控制項 自動適應大小            m_Photo.GetClientRect (rc);            m_Photo.ClientToScreen(rc);            ScreenToClient(rc);            long Width  = 0;            long Height = 0;            m_Pic.m_IPicture->get_Width(&Width);            m_Pic.m_IPicture->get_Height(&Height);            float f=Width/(float)Height;            long w=f*(rc.bottom-rc.top);            m_Photo.SetWindowPos (NULL,rc.left,rc.top,w,rc.Height(), SWP_NOMOVE);            m_Photo.GetClientRect (rc);            CClientDC dc(&m_Photo);            m_Pic.UpdateSizeOnDC (&dc);            rc.left--;            rc.top--;            m_Pic.Show (&dc,rc);            }        }    }


相關文章

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.