Writing BLOBS to ODBC

Posted on 2004-11-15
Last Modified: 2012-05-05
Can someone show me how to store BLOBs in an ODBC database, using VC++ & .NET?
The databases I want to connect to are:
   either Access or SqlServer  (do either of these support BLOBs?)


Question by:jpalumbos
    LVL 19

    Expert Comment

    Both SQL Server and Access can support BLOBs.  How are you trying to access the database - ADO .NET, ADO, OLE DB, ATL database helper classes, other...?  Mainly, are you using managed or unmanaged C++ and what API are you using/want to use for data access?

    Author Comment

    I'm just using ODBC, with SQL statements to access the database.
    I don't know the answers to the other questions you're asking.  (managed c++?  api for data access?)
    LVL 19

    Accepted Solution

    Since you don't know what managed C++ is, I'll assume you are using standard C++.  In that case, you can use ADO to access the database.  SQL Server, and probably Oracle, have other ways of accessing BLOB data as well, but this will work for Access too.  For it to work, you will need to have:

    #include <atldbcli.h>
    #include <iostream>

    #undef EOF
    #import "C:\Winnt\System32\dllcache\msado15.dll"

    Also, you will need to initialize COM (like CoInitialize(0);) prior to using ADO calls.  ADO is well documented in MSDN:

    If this is not what you were looking for, let me know.  The code used a database that was set up with an ODBC DSN  called "BLOB Test" and had a table called "tblPics" with an autonumber column and then a "Filename" and "Picture" column.  The "Picture" column was a binary data column that stored the contents of an image file (like a jpg).

         ADODB::_ConnectionPtr     con = NULL;
         ADODB::_RecordsetPtr     rec = NULL;
         HRESULT               hr = S_OK;
         VARIANT               *vRecordsAffected = NULL;

         // Create and open the connection
         hr = con.CreateInstance (__uuidof(ADODB::Connection));
         con->Open ("DSN=BLOB Test;", "", "", 0);

         // Execute a query and move to the first record of the result
         rec = con->Execute("SELECT * FROM tblPics;", vRecordsAffected, 1);

         // Get the picture data (a jpg file)
         _variant_t idx = "Picture";
         ADODB::FieldPtr pf = rec->Fields->Item["Picture"];
         long sz = pf->ActualSize;
         _variant_t val = pf->GetChunk(sz);
         // The result should be a SAFEARRAY of bytes
         if ( val.vt == (VT_UI1 | VT_ARRAY) )
             std::cout << "Got BLOB of size " << val.parray->rgsabound->cElements << std::endl;

         // Close the recordset

         // Create a new recordset on the connection

         // Populate the recordset
         _variant_t cmd = "SELECT * FROM tblPics;";
         _variant_t varcon;
         varcon.pdispVal = con;
         varcon.vt = VT_DISPATCH;
         rec->Open(&cmd, &varcon, ADODB::adOpenDynamic, ADODB::adLockOptimistic, 0);

         // Add a new record and copy the picture we just read into a new record
         rec->Fields->Item["Filename"]->Value = "bogus.jpg";
         rec->Fields->Item["Picture"]->Value = val;

         // Clean up

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now