void COpenFileDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CFile imagefile;
if(0 == imagefile.Open(_T("E:\\test.bmp"),CFile::modeRead))
return;
_RecordsetPtr pRs = NULL;             
_ConnectionPtr pConnection = NULL;
_variant_t varChunk;
HRESULT hr;
BYTE* pbuf;
long nLength = imagefile.GetLength();
pbuf = new BYTE[nLength+2];
if(pbuf == NULL)
return;                             //allocate memory error;
imagefile.Read(pbuf,nLength);          //read the file into memory BYTE *pBufEx;
pBufEx = pbuf;
//build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); for (long i = 0; i < nLength; i++)
SafeArrayPutElement (psa, &i, pBufEx++);
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa; _bstr_t strCnn("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PassWord =caxa;Initial Catalog=test;Data Source=CAXA-F5F9BD9A35");    
try
{
//Open a connection
pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection->Open(strCnn,"","",NULL);   //Connect a DataBase
pRs.CreateInstance(__uuidof(Recordset));
pRs->Open("Im_Info",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);  //Open a Table //      pRs->AddNew();        
pRs->Fields->GetItem("Pr_Info")->AppendChunk(varBLOB);        
pRs->Update();
pRs->Close();
pConnection->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format(_T("Source : %s \n Description : %s\n"),(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);     
}
}
void COpenFileDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open("select * from Im_Info",_variant_t((IDispatch*)theApp.m_pConnection,true), adOpenDynamic,adLockOptimistic,adCmdText);
if (!m_pRecordset->adoEOF)
{
m_pRecordset->MoveFirst();
}
long nSize = m_pRecordset->GetFields()->GetItem("Pr_Info")->ActualSize;
if(nSize > 0)
{
_variant_t varBLOB;
varBLOB = m_pRecordset->GetFields()->GetItem("Pr_Info")->GetChunk(nSize);
try
{
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
if(BYTE *pBuffer = new BYTE [nSize+1]) ///重新申请必要的存储空间
{
char *pBuf = NULL;
HRESULT hr;
hr= SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(pBuffer,pBuf,nSize); ///复制数据到缓冲区m_pBMPBuffer
SafeArrayUnaccessData (varBLOB.parray);
(LoadPictureData(pBuffer, nSize)); delete [] pBuffer;
pBuf=0;
CWnd * pwndPic = GetDlgItem(IDC_STATIC2);
// CStatic *pwndPic;
// pwndPic = (CStatic*)GetDlgItem(IDC_STATIC);
CDC * pDC = pwndPic->GetDC();
CRect rect;
pwndPic->GetClientRect(&rect);
//pwndPic->GetWindowRect(rect);
pwndPic->ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE);

Show(pDC, rect);
}
}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format(_T("Source : %s \n Description : %s\n"),(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);     
}
}
}
BOOL COpenFileDlg::LoadPictureData(BYTE *pBuffer, int nSize)
{
BOOL bResult = FALSE; HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize); if(hGlobal == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
//MessageBoxEx(hWnd, _T("Can not allocate enough memory\t"), ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
} void* pData = GlobalLock(hGlobal);
memcpy(pData, pBuffer, nSize);
GlobalUnlock(hGlobal); IStream* pStream = NULL; if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
HRESULT hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture);
if(hr == E_NOINTERFACE)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
//MessageBoxEx(hWnd, _T("IPicture interface is not supported\t"), ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
else // S_OK
{
pStream->Release();
pStream = NULL;
bResult = TRUE;
}
} FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release) return(bResult);
}
BOOL COpenFileDlg::Show(CDC *pDC, CRect DrawRect)
{ if (pDC == NULL || m_IPicture == NULL)
return FALSE; long Width  = 0;
long Height = 0;
m_IPicture->get_Width(&Width);
m_IPicture->get_Height(&Height); HRESULT hrP = NULL; hrP = m_IPicture->Render(*pDC,
DrawRect.left,                  // Left
DrawRect.top,                   // Top
DrawRect.right - DrawRect.left, // Right
DrawRect.bottom - DrawRect.top, // Bottom
0,
Height,
Width,
-Height,
&DrawRect);
//pDC->m_hDC
if (SUCCEEDED(hrP)) 
return(TRUE); HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
UpdateData(FALSE);
// MessageBoxEx(hWnd, _T("Can not allocate enough memory\t"), ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
//return(FALSE);}
Button1是写入数据库,Button2是读取
各位高人来帮忙看看问题在哪里~~~谢谢