🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.database.ado
Article #86

Re: Read/write BLOB fields using ADO

#86
From: Michael Corby
Date: Mon, 18 Feb 2008 23:17
148 lines
5165 bytes
Hi Mike

Hope you can get what you want from the following code snippet
from one of my test programs.


   int recNo;
   String pictureName, outPut, ext, picInd, colour, blank;

   blank = "";

   wDataModule->tPlants->Active = false;
   wDataModule->tPlants->Active = true;

   TField *field = (wDataModule->tPlants->FieldByName("JPGBMPImage"));
   TBlobField *blobField = dynamic_cast<TBlobField*>(field);
   TMemoryStream *stream = new TMemoryStream;

   recNo = 1;
   for (wDataModule->tPlants->First(); !wDataModule->tPlants->Eof;
        wDataModule->tPlants->Next())
   {
      wDataModule->tPlants->Edit();

      switch(recNo)
      {
         case 1  : pictureName = "C:\\MC\\Images\\Cus1.bmp";
                   colour = ExtractFileName(pictureName);
                   ext = ExtractFileExt(pictureName);
                   picInd = ext.SubString(2,3);
                   break;
         case 2  : pictureName = "C:\\MC\\Images\\Tulips.jpg";
                   colour = ExtractFileName(pictureName);
                   ext = ExtractFileExt(pictureName);
                   picInd = ext.SubString(2,3);
                   break;
         case 3  : pictureName = "C:\\MC\\Images\\Cus5.bmp";
                   colour = ExtractFileName(pictureName);
                   ext = ExtractFileExt(pictureName);
                   picInd = ext.SubString(2,3);
                   break;
         case 4  : pictureName = "C:\\MC\\ScanTest.bmp";
                   colour = ExtractFileName(pictureName);
                   ext = ExtractFileExt(pictureName);
                   picInd = ext.SubString(2,3);
                   break;
         case 5  : picInd = blank;
                   colour = blank;
                   break;
      }
      // No picture for Record 5
      if (recNo != 5)
      {
        stream->LoadFromFile(pictureName);
        stream->Position = 0;
        blobField->LoadFromStream(stream);
      }

      wDataModule->tPlants->FieldValues["Colour"] = colour;
      wDataModule->tPlants->FieldValues["JPGBMPInd"] = picInd;
      wDataModule->tPlants->Post();
      recNo++;
   }
   wDataModule->tPlants->Active = false;

   delete stream;

   wSecondForm->Memo1->Lines->Clear();
   wSecondForm->Memo1->Font->Color = clRed;
   outPut = "Images Loaded to Database record";
   wSecondForm->Memo1->Lines->Add(outPut);
   Sleep(2000);
   wSecondForm->Memo1->Font->Color = clBlack;


Michael Corby









"Mike K" <nospam@nospam.net> wrote in message 
news:47b9e51b$1@newsgroups.borland.com...
> I'm trying to retrieve a block of binary data (~5k bytes) from a SQL
> Server database table. The column name is 'data'.
>
> Using TADOQuery, I want to read the record, and write the data from the
> result set into memory. Also, I need to do the reverse (write the record). 
> This should not be rocket science.
>
> Here's what I have so far after a day and a half experimenting with
> snippets from old newsgroup posts:
>
>
> <snip>
> typedef struct{
> ...
> }CONFIG_DATA;
> CONFIG_DATA *cp;
> TADOQuery *iQuery;
> TBlobStream *bptr;
>
>
> iQuery->Active = false;
> iQuery->SQL->Clear();
> // just get one record:
> iQuery->SQL->Add("select data from config_data where ID = 3001");
> iQuery->Active = true;
>
> // got a runtime error before I put this line in:
> iQuery->Edit();
>
>
> bptr = 
> dynamic_cast<TBlobStream*>(iQuery->CreateBlobStream(iQuery->FieldByName("data"),bmRead));
>
> if(bptr) bptr = ReadBuffer(cp,bptr->Size);
> // home free if you get here!
>
> <end snip>
>
> Works fine until I get to CreateBlobStream(), which returns a null 
> pointer.  I've tried tons of methods to create the TBlobStream pointer,
> none of which work, many of which won't even compile (taken straight from 
> BCB5 help).
>
> This should not be difficult, but I'm having no luck.  I have been using 
> ADO database objects with BCB5 for years, just never had large binary 
> fields.
>
> Any kind soul who is willing to either give me the few lines of code I 
> need, or point me to a SPECIFIC WORKING example anywhere cyberspace, I 
> will happily add you to my Christmas card list (it's too late for a 
> valentine).
>
> TIA
>
> Mike
>
>
>
>
>
> 
Technical Details
Message-ID: <47ba118a@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <47b9e51b$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10698b,1c341601eb4e7a8a
X-Google-Attributes: gid10698b,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Michael Corby" <mcorby@sapo.pt>
Newsgroups: borland.public.cppbuilder.database.ado
References: <47b9e51b$1@newsgroups.borland.com>
Subject: Re: Read/write BLOB fields using ADO
Date: Mon, 18 Feb 2008 23:17:04 -0000
Lines: 150
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 85.243.1.89
Message-ID: <47ba118a@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1203376522 85.243.1.89 (18 Feb 2008 15:15:22 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail