如何存储自定义格式的图片文件到SQL中及如何用Vc来读取

1982xcc 2003-12-12 07:52:22
如题
...全文
112 7 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
1982xcc 2003-12-14
请高手指点,急忙!
  • 打赏
  • 举报
回复
AkiraChing 2003-12-14
储存图片

rs.CreateInstance(__uuidof(Recordset));

rs->Open("相片",_variant_t((IDispatch *)m_pConn,true), adOpenKeyset,
adLockOptimistic, adCmdTable);
rs->AddNew();
_variant_t var;
COleDateTime tDate;
m_datePic.GetTime(tDate);
var.vt=VT_DATE;
var.date=tDate;
rs->GetFields()->GetItem("拍摄日期")->Value=var;
var.Clear();
var.vt=VT_BSTR;
var.bstrVal=strTheme.AllocSysString();
rs->GetFields()->GetItem("主题名称")->Value=var;
var.bstrVal=strId.AllocSysString();
rs->GetFields()->GetItem("照片 ID")->Value=var;
CFile file;
if( !file.Open( m_strPicfile, CFile::modeRead) )
return ;
DWORD uFilesize =(DWORD) file.GetLength();
char* pBuffer = new char[uFilesize + 1];
if(pBuffer!=NULL)
{
ZeroMemory(pBuffer,uFilesize + 1);
VERIFY(file.Read(pBuffer,uFilesize)==uFilesize);

VARIANT varImage;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];

rgsabound[0].lLbound = 0;
rgsabound[0].cElements = uFilesize;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)uFilesize; i++)
SafeArrayPutElement (psa, &i, pBuffer++);
varImage.vt = VT_ARRAY | VT_UI1;
varImage.parray = psa;
rs->GetFields()->GetItem("图片")->AppendChunk(varImage);

}

rs->Update();
pBuffer=pBuffer-uFilesize;
if(pBuffer)
{
delete [] pBuffer;
pBuffer=NULL;
}
MessageBox("添加数据成功!","提示",MB_OK|MB_ICONINFORMATION);
}
catch (_com_error e)
{
PrintComError(e);
}

if(rs->State)
rs->Close();
  • 打赏
  • 举报
回复
1982xcc 2003-12-13
"// 文件可以是任意格式,但是数据库中必须有文件的大小"
数据库中图片定义为image格式,如何指定其大小,我想完成一个通过文件列表框打开
选取一个图片,存储。从数据库读出 显示的功能,还请指教。

PS 数据库操作新手,还请将文件操作的代码完整些,谢谢
  • 打赏
  • 举报
回复
AkiraChing 2003-12-13
HGLOBAL hgImageMem = NULL;
LPVOID lpvImageMem = NULL;

long lImageSize = rs->GetFields()->GetItem("图片")->ActualSize;

if(lImageSize > 0)
{
_variant_t varImage;
varImage = rs->GetFields()->GetItem("图片")->GetChunk(lImageSize);
if(varImage.vt == (VT_ARRAY | VT_UI1))
{

hgImageMem = GlobalAlloc(GHND, lImageSize);
if(hgImageMem!=NULL)
{
lpvImageMem = GlobalLock( hgImageMem);
char *pBuffer= NULL;
SafeArrayAccessData(varImage.parray,(void **)&pBuffer);
CopyMemory(lpvImageMem,pBuffer,lImageSize);
SafeArrayUnaccessData (varImage.parray);
CFile file("a.jpg",CFile::modeWrite|CFile::modeCreate);
file.Write(lpvImageMem,lImageSize);
file.Close();
}
}
}

GlobalUnlock(hgImageMem);
lpvImageMem=NULL;
GlobalFree(hgImageMem);
  • 打赏
  • 举报
回复
nava 2003-12-12
#define FILE_MAX_LENGTH 5242880 // 5M
// 文件可以是任意格式,但是数据库中必须有文件的大小

HANDLE hFile;
char* strBuff = new char[FILE_MAX_LENGTH];
DWORD dwFileSize;
DWORD dwRead;
hFile = CreateFile("c:\\2.bmp", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
if (!ReadFile(hFile, strBuff, dwFileSize, &dwRead, NULL))
return;
}
CloseHandle(hFile);
}
// 向数据库中写入数据,省去一些步骤应该没问题吧
rs.AppendChunk(_T("FILE"), strBuff);
rs.SetFieldValue(_T("FILELENGTH"), dwFileSize);

delete[] strBuff;

// 从数据库中读取
HANDLE hFile;
DWORD dwFileSize;
DWORD dwRead;
char* strBuff = new char[FILE_MAX_LENGTH];

rs.GetChunk(_T("FILE"), strBuff);
rs.GetFieldValue(_T("FILELENGTH"), dwFileSize);

hFile = CreateFile(_T("C:\\1.bmp"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (!WriteFile(hFile, strBuff, dwFileSize, &dwRead, NULL))
return;
CloseHandle(hFile);
}

delete[] strBuff;
  • 打赏
  • 举报
回复
1982xcc 2003-12-12
是AppendChunk() 和 GetChunk ?
里面的解释少得可怜,有没有具体详细的用法说明呢?
  • 打赏
  • 举报
回复
smch 2003-12-12
GetTrunk
AppendTrunk
在SQL Server中的联机文档查
  • 打赏
  • 举报
回复
相关推荐
发帖
数据库
加入

3967

社区成员

VC/MFC 数据库
申请成为版主
帖子事件
创建了帖子
2003-12-12 07:52
社区公告
暂无公告