Hunter
June 3rd, 1999, 04:38 PM
How do I read a OLE field(Picture under SQL) and display it in a control?
I need to read a picture stored in a database (SQL in my case).
and how do I write a picture to a database?
please send me some pieces of code
R.F.L.
Brett Liu
June 3rd, 1999, 08:02 PM
To put a bitmap file in to a database,first create a byte array,copy file in to that array,and then write it in database as a long binary record.
Here is some code,I hope it is helpful.
below m_rSet_l is a object of CDaoRecordSet,
filename can be any file,include bitmap :-)
But code below has a problem,If you only call this function once in a time,it goes ok,but
if you call it twice or more in a time,a error occours :-(((,can you tell me why ?
extern "C" BOOL WINAPI PawnImg(char* u_id,
char* filename)
{ CString ID;
ID.Format("[L_PHOTO].[LESSEE_ID]='%s'",u_id);
if(!m_rSet_l.IsOpen()) m_rSet_l.Open();
if(m_rSet_l.Find(AFX_DAO_FIRST,ID)){
m_rSet_l.Close();
CString ID_message;
// ID_message="There has no "+ID+" in this Database";
ID_message="There has already has a ID "+ID+"in this database,so change another";
AfxMessageBox(ID_message);
return FALSE;
}
// m_rSet_l.Edit();
m_rSet_l.AddNew();
m_rSet_l.SetFieldValue("[LESSEE_ID]",u_id);
FILE* f=fopen(filename,"rb");
fseek(f,0,SEEK_END);
unsigned long len=ftell(f);
if(len>GlobalSize(m_rSet_l.m_PIC.m_hData))
m_rSet_l.m_PIC.m_hData=GlobalAlloc(0,len);
char* p=(char*)GlobalLock(m_rSet_l.m_PIC.m_hData);
fseek(f,0,SEEK_SET);
fread(p,1,len,f);
fclose(f);
GlobalUnlock(p);
m_rSet_l.m_PIC.m_dwDataLength=len;
m_rSet_l.SetFieldDirty(&m_rSet_l.m_PIC);
m_rSet_l.SetFieldNull(&m_rSet_l.m_PIC,FALSE);
m_rSet_l.Update();
::GlobalFree(m_rSet_l.m_PIC.m_hData);
m_rSet_l.Close();
return TRUE;
}
regards
brett