程序员人生 网站导航

往数据库中添加照片

栏目:数据库应用时间:2015-01-18 10:14:55

作者: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)) //打开了1个jpg文件         {                 int nSize = f.GetLength(); //先得到jpg文件长度             BYTE * pBuffer = new BYTE [nSize]; //按文件的大小在堆上申请1块内存             if (f.Read(pBuffer, nSize) > 0 )    //把jpg文件读到pBuffer(堆上申请1块内存)             {                  BYTE *pBuf = pBuffer; ///下面这1大段是把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); //选中行的第1列的文字     _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);             }         }     }


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐