We help IT Professionals succeed at work.

How do i store/read and image from an MS SQL Server table using CLongBinary in VC++

Inacio
Inacio asked
on
I am trying to store an image file into an SQL table. I am using MS SQL Server as the back end and VC++ as the front end. I am using CRecordset to connect to the database and browse through the records. MSDN says to use either CByteArray or CLongBinary to store large datatypes. But i can't figure out how to implement it. Please help.
Thanks.
Comment
Watch Question

ekc

Commented:
The large binaries (like images) are rarely stored directly in the databases.
Normally you store the images to some directories (like normal files) and use the database to keep the image paths.
This way is much easier to implement (treat the images like normal files) and especially to maintain.
CERTIFIED EXPERT
Author of the Year 2009

Commented:
hi Inacio,
I addresses reading and writing BLOBS (Binary Large OBjects) in this Q:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20184283

You will find all the code you need there.  It is not too difficult.

=-=-=-=-=-=-=-
ekc,
As you will see in the discussion from that link, there are several good reasons to store large objects into a database rather than into the filesystem.  The client app already has an open database connection but may or may not have permission (or any real desire!) to read disk files from the remote server.

When BLOB access is implemented for web handling, you can package your entire site -- text, images, scripts, etc -- into one database file (I used that technique with a Sybase NetDynamo once and it was pretty slick)

-- Dan
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Oops, the link describes how to do BLOBS in ADO, but it sounds like you may be using ODBC acces and CDatabase/ CRecordset class.

If so, it is considerably easier than with ADO.  Just define the BLOB field as type 'image.'  WHen you use ClassWizard to create your CRecordset-derived object, it will add a line like:

  CLongBinary abBinaryData;

to the header and

   RFX_LongBinary(pFX, _T("[abBinaryData]"), m_abBinaryData);

to the DoFieldExchange() fn in the cpp file.  This does all of the work!

Have you already tried this technique and and run into specific problems?  If so, describe what is happening.  

Do you need help in reading the file into the CLongBinary member element?  

-- Dan

Author

Commented:
I managed to do saving image into an SQL table using CByteArray. Thanks all for the comments.
CERTIFIED EXPERT
Author of the Year 2009

Commented:
hi Inacio,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Hi Inacio,
If no comment helped you, then please delete this question.  Thanks!

-- Dan
CERTIFIED EXPERT
Author of the Year 2009

Commented:
hi Inacio,
Do you have any additional questions?  Do any comments need clarification?

-- Dan

Author

Commented:
I have already found a solution to the problem a long time back and i had deleted this question, I do not know why it isn't deleted. Please do not send any answers. Thanks !
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Inacio,
Please inform Community Support that you have solved this problem yourself.  To do that, post a 0-pt Q here:

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

In that q, refer to this question # ( http:Q.20187938.html )

-- Dan
Per request, points refunded and question closed.

Netminder
CS Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.