如何从数据库中读取CLongBinary字段并将其写入CFile对象

How can I read a CLongBinary field from my database and write it into a CFile object?

本文关键字:对象 CFile 字段 数据库 CLongBinary 读取      更新时间:2023-10-16

im使用CRecordset从我的SQL Server获取数据。一个表存储一个二进制文件(pdf,odt,…)。我的VisualStudio将列映射到CLongBinary字段。

如何从CLongBinary字段读取并打开文件

im使用MFC的一些古代版本4.2VisualStudio6.0

这样做。这只会给你一个想法。我还没有测试代码。

CLongBinary myfield ;
... retrive myfield from database here

BYTE *dataptr = (BYTE*)GlobalLock(myfield.m_hData) ;
// now dataptr points to your raw data, and myfield.m_dwDataLength is the length of that data
CString tempname = ... create temporary filename somewhere
CFile myfile ;
myfile.Open(tempfilename, CFile::modeCreate|CFile::modeWrite);
myfile.Write(dataptr, myfield.m_dwDataLength) ,
myfile.Close() ;
GlobalUnlock(myfield.m_hData) ;
ShellExecute(NULL, _T("open"), tempfilename, NULL, NULL, SW_SHOW) ;