//add pic to database; the column save image type must image
void CBmpDlgDlg::WriteImage2DB(CString path)
{
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
try
{
CFile fileAdd;
if (fileAdd.Open ("Debug\nv.bmp",CFile::modeRead) == 0) // open the file
return;
_variant_t varChunk;
long m_nFileLen = fileAdd.GetLength();
BYTE * m_pBMPBuffer;
m_pBMPBuffer = new BYTE[m_nFileLen];
if (m_pBMPBuffer == NULL)
return;
fileAdd.Read(m_pBMPBuffer,m_nFileLen);
char * pBuf = (char *)m_pBMPBuffer;
VARIANT varBLOB;
SAFEARRAY * psa;
SAFEARRAYBOUND rgsabound[1];
/****/
CString strSQL;
_bstr_t bstr;
strSQL.Format(_T("select count(*) as num, Max(ID) as maxid from pic"));
_RecordsetPtr m_pRecordset;
try
{
m_pRecordset = m_AdoConn.GetRecordset((_bstr_t)strSQL);
}
catch (_com_error &e)
{
AfxMessageBox("获取最大id失败");
}
//从RecordSet中获取数据数目和当前数据库中最大的ID。
long num =m_pRecordset->GetCollect("num");
long maxid;
try
{
if (num != 0)
{
maxid = m_pRecordset->GetCollect("maxid");
}
else
{
maxid = 0;
}
}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
}
strSQL.Format(_T("Select * from pic where ID = %d"), maxid);
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset = m_AdoConn.GetRecordset((_bstr_t)strSQL);
m_pRecordset-> AddNew();
m_pRecordset-> PutCollect ("name", _variant_t ("Mike")); //add data to db by column
if (pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = m_nFileLen;
psa = SafeArrayCreate (VT_UI1, 1, rgsabound); /// Create a SAFEARRAY object
for (long i = 0; i<(long)m_nFileLen; i++)
SafeArrayPutElement (psa,&i, pBuf++); /// pBuf point binary data saved to the SAFEARRAY object psa,
varBLOB.vt = VT_ARRAY | VT_UI1; /// varBLOB type is set to an array of type BYTE
varBLOB.parray = psa; /// for varBLOB variable assignment
m_pRecordset->GetFields ()->GetItem ("pic")->AppendChunk(varBLOB) ;/// BLOB type data
}
m_pRecordset->Update(); //update data
m_AdoConn.ExitConnect();
AfxMessageBox("添加数据成功!");
}
}
//get pic data from databse and path gen pic
int CSS628DDlg::CreateImageByBinData(CString path,CString m_IDNum)
{
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
int m_retv=0;
try
{
CString strSQL;
_bstr_t bstr;
_RecordsetPtr m_pRecordset;
/****************************************************/
//Read from the database out of the picture
strSQL.Format(_T("Select * from IdCard where IDNum = '%s'"),m_IDNum);
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset = m_AdoConn.GetRecordset((_bstr_t)strSQL);
long lDataLength = m_pRecordset->GetFields()-> GetItem(_variant_t ("Img"))-> ActualSize;
m_AdoConn.m_pRecordset->adoEOF;
if(!m_AdoConn.m_pRecordset->adoEOF)
{
m_strIDNum=m_IDNum;
if (lDataLength> 0)
{
_variant_t varBLOB;
varBLOB = m_pRecordset->GetFields ()->GetItem(_variant_t ("Img"))->GetChunk (lDataLength);
if (varBLOB.vt == (VT_ARRAY|VT_UI1) && varBLOB.vt != VT_EMPTY && varBLOB.vt != VT_NULL)
{
BYTE * pBuf = NULL;
pBuf = (BYTE *) GlobalAlloc (GMEM_FIXED, lDataLength);
SafeArrayAccessData (varBLOB.parray, (void **)&pBuf);
CFile outFile(path,CFile::modeCreate | CFile::modeWrite); // construct a new file, if the file exists, the length becomes 0
outFile.Write(pBuf,lDataLength);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
}
else
{
//zb.bmp文件如果存在则删除
if(GetFileAttributes("zp.bmp")!=-1)
DeleteFile("zp.bmp");
}
m_retv=1;
UpdateData(FALSE);
}
/***********************************/
m_AdoConn.ExitConnect();
}
catch (_com_error &e)
{
//AfxMessageBox(e.Description());
}
return m_retv;
}