通过ADO把照片以二进制方式写入到数据库保存;
从下载二进制文件到文件
////////////////////数据库相关 头文件里面定义//////////////////////////////
CString m_strConnection; // 数据库连接字符串
CADODatabase m_Db; // 数据库连接
CADORecordset m_Rs; // 数据库记录集
//写入图片到数据库image类型字段
if(!m_Db.Open(m_strConnection)) // 打开数据库
{
MessageBox("打开数据库失败!");
return;
}
m_Rs = CADORecordset(&m_Db);// 创建记录集
//Image_Path从配置文件读入
CFile file;
int fileCount = CountDirectory(Image_Path);//通过传人路径,获取该目录下面的文件个数
for (int i=1;i<fileCount;i++)
{
CString filename;
filename.Format("image\\image%d.jpg",i);
if(!file.Open(filename,CFile::modeRead | CFile::typeBinary))
return;
int dwLo=file.GetLength();
BYTE *m_pImagBuffer=new BYTE[dwLo];
ZeroMemory(m_pImagBuffer,dwLo);
file.Read(m_pImagBuffer,dwLo);
file.Close();
// BOOL m_b = file.Open("1.dat",CFile::modeWrite | CFile::modeCreate |CFile::typeBinary);
// file.Write(m_pImagBuffer,dwLo);
// file.Close();
strQuery.Format("Select * From ImageTest");
m_Rs.Open(strQuery,CADORecordset::cadoOpenEnum::openQuery);
m_Rs.AddNew();
CString strTime;
SYSTEMTIME systm;
GetLocalTime(&systm);
strTime.Format("%4d-%02d-%02d %02d:%02d:%02d",\
systm.wYear, systm.wMonth, systm.wDay,\
systm.wHour, systm.wMinute, systm.wSecond);
m_Rs.SetFieldValue("Date",strTime);
m_Rs.AppendChunk("ImageData",m_pImagBuffer,dwLo);
m_Rs.Update();
m_Rs.Close();
delete []m_pImagBuffer;
}
m_Db.Close();
//读取数据库二进制文件保存到本地
int index=0; //记录个数
if(!m_Db.Open(m_strConnection)) // 打开数据库
{
MessageBox("打开数据库失败!");
return;
}
m_Rs = CADORecordset(&m_Db);// 创建记录集
CString strFilePath;
//strFilePath = Image_Path + "\\BMP";
strFilePath = "OUT_BMP";
::CreateDirectory(strFilePath,NULL); //创建目录
// CString strNameCount;
// int nRowCount;
// strNameCount.Format("SELECT COUNT(ImageData) FROM ImageTest");
//
// if (m_Rs.Open(strNameCount,CADORecordset::cadoOpenEnum::openQuery))
// m_Rs.GetFieldValue(0,nRowCount); //获取记录个数
// m_Rs.Close();
strQuery.Format("Select ImageData From ImageTest");
if(m_Rs.Open(strQuery,CADORecordset::cadoOpenEnum::openQuery))
{
while(!(m_Rs.IsEOF()))
{
CString filename;
filename.Format("%s\\image%d.png",strFilePath,index);
m_ImgNameMap[index]=filename; //把输出的所有名字放到容器里面
CADOFieldInfo fldInfo;
m_Rs.GetFieldInfo("ImageData",&fldInfo);
BYTE *m_pImagBuffer = new BYTE[fldInfo.m_lSize];
m_Rs.GetChunk("ImageData",m_pImagBuffer);
CFile outFile(filename,CFile::modeCreate | CFile::modeWrite);
outFile.Write(m_pImagBuffer,fldInfo.m_lSize);
outFile.Close();
m_Rs.MoveNext();
index++;
delete []m_pImagBuffer;
}
m_Rs.Close();
}
m_Db.Close();
软件实例截图:
软件实例下载地址:
http://download.csdn.net/detail/suifeng1984114/7835095
软件截图
本文出自 “随风” 博客,请务必保留此出处http://suifeng.blog.51cto.com/268163/1546263