如题,我的环境是vc+mysql+ado,我在mysql中见了表printtemp,其中有图片字段photo,类型为longblob,程序是的实现过程是这样的,首先获取了图片的目录,然后调用函数将图片存储,但是这个函数该如何写?我现有的函数是这样的,
Directory=ctv->imagepath[i];
type=pDoc->GetTypeFromFileName((LPCTSTR)Directory
SaveImageToDB(Directory,23);
其中23是图片在表中存储的索引值。SaveImageToDB()函数如下:
CFile imagefile;
CFileStatus fileStatus;
if(0 == imagefile.Open(Directory,CFile::modeRead))
return;
imagefile.GetStatus(fileStatus);
// long nLength = imagefile.GetLength();
long nLength =(ULONG)fileStatus.m_size;
CString s;
s.Format("%d",nLength);
// MessageBox(s);
// BYTE *pbuf = NULL;
HGLOBAL hGlobal = GlobalAlloc(GPTR,nLength);
LPVOID lpData = GlobalLock(hGlobal);
//存
// pbuf = new BYTE[nLength];
if(lpData == NULL)
return; //allocate memory error;
imagefile.Read(lpData,nLength); //read the file into memory
theApp.pRs->AppendChunk(nIndex,lpData,nLength);
theApp.pRs->Update();
// delete []pbuf;
GlobalUnlock(lpData);但是执行到AppendChunk(nIndex,lpData,nLength)这里的时候就报不支持操作,是mysql数据库报的错,我用的是ado
Directory=ctv->imagepath[i];
type=pDoc->GetTypeFromFileName((LPCTSTR)Directory
SaveImageToDB(Directory,23);
其中23是图片在表中存储的索引值。SaveImageToDB()函数如下:
CFile imagefile;
CFileStatus fileStatus;
if(0 == imagefile.Open(Directory,CFile::modeRead))
return;
imagefile.GetStatus(fileStatus);
// long nLength = imagefile.GetLength();
long nLength =(ULONG)fileStatus.m_size;
CString s;
s.Format("%d",nLength);
// MessageBox(s);
// BYTE *pbuf = NULL;
HGLOBAL hGlobal = GlobalAlloc(GPTR,nLength);
LPVOID lpData = GlobalLock(hGlobal);
//存
// pbuf = new BYTE[nLength];
if(lpData == NULL)
return; //allocate memory error;
imagefile.Read(lpData,nLength); //read the file into memory
theApp.pRs->AppendChunk(nIndex,lpData,nLength);
theApp.pRs->Update();
// delete []pbuf;
GlobalUnlock(lpData);但是执行到AppendChunk(nIndex,lpData,nLength)这里的时候就报不支持操作,是mysql数据库报的错,我用的是ado
解决方案 »
- VC调试下,怎么样才能看到变量的值和地址
- 为什么调整窗口大小后,程序占用内存的数量在增加?
- Getwindowtheadprocessid(),获取进程id出错阿。
- 判断键盘消息的问题
- 在建立Win32 Application 出现的两个错误信息,再现等待,解决后马上结帖
- socket的问题,如何知道出错???
- 使用__stdcall的dll的问题
- newhand的问题
- 在树控件中,如何使与指定文本相同的那个树节点(包括根节点)选中?
- 一个关于在局域网中传输文件的送分问题!
- 急!我想在mfc中使用ADO连接sql server数据库,如何设置,怎么连接,怎么建表,多谢!
- CreateUrlCacheEntry 在安装IE7的机器上执行失败
http://www.vckbase.com/document/viewdoc/?id=719
http://www.vckbase.com/document/viewdoc/?id=252 另外,用如下代码把你的代码包起来
try
{
//你的代码
}
catch(_com_error &e)
{
AfxMessageBox(e.Description());
}
看看能否捕获异常??
BOOL CADORecordset::AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
{
HRESULT hr;
_variant_t varChunk;
long lngOffset = 0;
UCHAR chData;
SAFEARRAY FAR *psa = NULL;
SAFEARRAYBOUND rgsabound[1]; try
{
//Create a safe array to store the array of BYTES
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nBytes;
psa = SafeArrayCreate(VT_UI1,1,rgsabound); while(lngOffset < (long)nBytes)
{
chData = ((UCHAR*)lpData)[lngOffset];
hr = SafeArrayPutElement(psa, &lngOffset, &chData); if(FAILED(hr))
return FALSE;
lngOffset++;
}
lngOffset = 0; //Assign the Safe array to a variant.
varChunk.vt = VT_ARRAY|VT_UI1;
varChunk.parray = psa; hr = pField->AppendChunk(varChunk);
m_pRecordset->Update();
if(SUCCEEDED(hr)) return TRUE;
}
catch(_com_error &e)
{
//dump_com_error(e);
AfxMessageBox(e.Description());
////////////////////////////////////////这里是我参照你说的做的
return FALSE;
} return FALSE;
}最后弹出一个对话框,内容是 在此环境中不允许操作
因为需求所以才做的数据库的移植单步调试到 我输入afxmessagebox的上一句,那句本来是不屏蔽的,是我为了加messagebox才屏蔽的
是什么问题呢?
不是。。在进入catch(_com_error &e)之前,最后是运行哪条??
{
_variant_t vtIndex;
vtIndex.vt = VT_I2;
vtIndex.iVal = nIndex; FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex); return AppendChunk(pField, lpData, nBytes);
}BOOL CADORecordset::AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
{
HRESULT hr;
_variant_t varChunk;
long lngOffset = 0;
UCHAR chData;
SAFEARRAY FAR *psa = NULL;
SAFEARRAYBOUND rgsabound[1]; try
{
//Create a safe array to store the array of BYTES
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nBytes;
psa = SafeArrayCreate(VT_UI1,1,rgsabound); while(lngOffset < (long)nBytes)
{
chData = ((UCHAR*)lpData)[lngOffset];
hr = SafeArrayPutElement(psa, &lngOffset, &chData); if(FAILED(hr))
return FALSE;
lngOffset++;
}
lngOffset = 0; //Assign the Safe array to a variant.
varChunk.vt = VT_ARRAY|VT_UI1;
varChunk.parray = psa; hr = pField->AppendChunk(varChunk);
m_pRecordset->Update();
if(SUCCEEDED(hr)) return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
AfxMessageBox(e.Description());
return FALSE;
} return FALSE;
}在dump_com_error(e)这弹出的messagebox说是“在此环境中不允许操作”
}
这个啊
是这个
因为我这样改了一下
hr = pField->AppendChunk(varChunk);
m_pRecordset->Update();
if(SUCCEEDED(hr)) {return TRUE;AfxMessageBox("hello");}}结果messagebox(hello)没有执行
仔细查一下使用mysql的blob有什么特殊要求??
我的mysql数据库是通过一个access2mysql工具转的,转过来后就用这种类型存储的,access中用的是ole对象。
这个应该和图片的类型没关系把?是不只和图片在mysql中的存贮类型有关啊