views:

204

answers:

1

I'm a C++ programmer using Borland's C++ Builder 5. I'm working on a database application and its causing me serious grief right now...

I'm using a Paradox 7.0 table with the C++ Builder and I simply can't find a way to insert data into BLOBs. I also can't save or view pictures using the TDBImage VCL component either.My latest foiled attempt was trying to save an image to a BLOB field using what seems to be an iron-clad piece of code.

//-----------------------------------------
Table1->Edit();
Open->Execute();
String file=Open->FileName;
ShowMessage(file);

TBlobField *blob; blob=new (TBlobField);

blob->FieldName="Image";
blob->LoadFromFile(file);

Table1->Post();
//-----------------------------------------

On compiling this code failed, siting that the BlobField doesn't have the dynamic object allocation function or something.

I'm also unable to add an OCX contoller for an OLE2 component nor save it in its own BLOB field.

Please, anyone, come to my aid

:'(

A: 

One thing that struck me, was your line:

TBlobField *blob; blob=new (TBlobField);

First of all, I'm not sure why you would use that syntax instead of:

TBlobField *blob = new TBlobField;

Secondly the TBlogField constructor take one argument, a pointer to a TComponent, which acts as the owner for the object, and is the object responsible for clearing the memory afterwards. One thing you could try is to do this:

TBlobField *blob = new TBlobField(Table1);

I must admit though that I haven't done too much database programming in C++ Builder, however, the above should work.

Edit: One more thing though, the way you use TBlobField is never associated with the table. You might find this article from about.com intersting: Storing Record Data in a BLOB Field [1]. It is written in Delphi, but should be easily ported to C++, due to the VCL framework.

You might try to use the following instead, it is not tested though, so you might encounter some problems in doing so, if so please tell me:

TField *field = Table1->FieldByName("image");
TBlobField *blob = dynamic_cast<TBlobField *>(field);
if (blob)
{
    blob->LoadFromFile(file);
}

[1] http://delphi.about.com/od/database/a/record2blob.htm

TommyA