🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.database.ado
4 messages
4 total messages Started by Mike K Mon, 18 Feb 2008 20:05
Read/write BLOB fields using ADO
#85
Author: Mike K
Date: Mon, 18 Feb 2008 20:05
55 lines
2483 bytes
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
Re: Read/write BLOB fields using ADO
#86
Author: 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
>
>
>
>
>
> 
Re: Read/write BLOB fields using ADO
#87
Author: Mike K
Date: Tue, 19 Feb 2008 00:58
5 lines
1011 bytes
Michael,

Thanks. Works fine and I'm on my way.  Newsgroups can be a beautiful thing.

Mike
Re: Read/write BLOB fields using ADO
#2
Author: Michael Corby
Date: Wed, 20 Feb 2008 09:55
12 lines
1281 bytes
Happy to help a fellow traveller on his merry way.

Michael

Mike K" <nospam@nospam.net> wrote in message 
news:47ba29c8$1@newsgroups.borland.com...
> Michael,
>
> Thanks. Works fine and I'm on my way.  Newsgroups can be a beautiful 
> thing.
>
> Mike 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads