VC2010 ADO writes binary data to a database to download binary data to a file

Source: Internet
Author: User

Through ADO, the photos are written in binary mode to the database to save;

From downloading a binary file to a file

Database related header file inside definition//////////////////////////////

CString m_strconnection; Database connection string

Cadodatabase m_db; //Database connection

Cadorecordset m_rs; //database record set



Write image to database image Type field

if (!m_db.open (m_strconnection))//Open Database

{

MessageBox ("Open database failed!") ");

Return

}


M_rs = Cadorecordset (&m_db);//create record set

Image_path reading from a configuration file

CFile file;

int filecount = Countdirectory (image_path);//through the descendant path, get the number of files under the directory

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 ();



Read database binaries saved to local

int index=0; Number of records


if (!m_db.open (m_strconnection))//Open Database

{

MessageBox ("Open database failed!") ");

Return

}

M_rs = Cadorecordset (&m_db);//create record set


CString strFilePath;

strFilePath = image_path + "\\BMP";

strFilePath = "Out_bmp";

:: CreateDirectory (Strfilepath,null); Create a Directory


CString Strnamecount;

int nrowcount;

Strnamecount.format ("Select COUNT (ImageData) from Imagetest");

//

if (M_rs.open (strnamecount,cadorecordset::cadoopenenum::openquery))

M_rs.getfieldvalue (0,nrowcount); Get the number of records

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; Put all the names of the outputs in the container.

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 ();


Software Examples:

Software Examples:

http://download.csdn.net/detail/suifeng1984114/7835095

Software

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/47/BC/wKioL1P_B5CBwOmpAAIcOFIHUo8187.jpg "title=" P.png " alt= "Wkiol1p_b5cbwompaaicofihuo8187.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/47/BA/wKiom1P_Bl-D2dD-AALGrnv915I002.jpg "title=" E.png " alt= "Wkiom1p_bl-d2dd-aalgrnv915i002.jpg"/>


This article is from the "Breeze" blog, be sure to keep this source http://suifeng.blog.51cto.com/268163/1546263

VC2010 ADO writes binary data to a database to download binary data to a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.