void Caccess_test_1Dlg::OnBnClickedButton3()//将偏振图像存入数据库
{
// TODO: 在此添加控件通知处理程序代码
if (!PathFileExists(_T(filename)))
{
AfxMessageBox(_T("数据库不存在!\r请创建数据库!"));
return;
}
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pConnection->Open(StrCmd, "", "", adModeUnknown);
m_pRecordset->Open("SELECT * FROM p ", _variant_t(m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format(_T("连接数据库失败!\r错误信息:%s"), e.ErrorMessage());
AfxMessageBox(errormessage);
return;
} CFile f;
CString FilePathName;
CFileException e; CFileDialog dlg(TRUE,NULL, NULL, , L"jpg Files (*.jpg)|*.jpg||", this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
if (dlg.DoModal() == IDOK)
{
FilePathName = dlg.GetPathName(); if (m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks... if (f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个jpg文件
{ int nSize = f.GetLength(); //先得到jpg文件长度
BYTE * pBuffer = new BYTE[nSize]; //按文件的大小在堆上申请一块内存 if (f.Read(pBuffer, nSize) > ) //把jpg文件读到pBuffer(堆上申请一块内存)
{ // +----------------------------------------------
BYTE *pBuf = pBuffer; ///下面这一大段是把pBuffer里的jpg数据放到库中
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[];
//m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pRecordset->AddNew();
if (pBuf)
{
rgsabound[].lLbound = ;
rgsabound[].cElements = nSize;
psa = SafeArrayCreate(VT_UI1, , rgsabound);
for (long i = ; i < (long)nSize; i++)
SafeArrayPutElement(psa, &i, pBuf++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->GetFields()->GetItem("偏振度图像")->AppendChunk(varBLOB);
}
m_pRecordset->Update();
// +----------------------------------------------
//(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示
delete[] pBuffer; //删掉堆上申请的那一块内存
pBuf = ; //以防二次乱用
}
f.Close();
} // CClientDC dc(this);
//m_Pic.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels
//m_Pic.Show(&dc, CRect(0, 50, m_Pic.m_Width, 50 + m_Pic.m_Height)); //显示出来看看 }
//Invalidate();
}

数据库ADO方式读取图片-LMLPHP

m_pRecordset.CreateInstance

数据库ADO方式读取图片-LMLPHP

05-11 22:22