Sybase, Inc. Navigational Bar

Powersoft Tools

Tip #11 - The BLOB in your Database!

When manually dealing with single fields in a Database, the rule of thumb is: "Use a WDataValue". Whether you need to insert an int or get a string from a Database, you will need to use a WDataValue to encapsulate the data.

The WDataValue class is for encapsulating nearly any data value of a particular cell in a data source (query object). The following constructors show the flexibility of the WDataValue class:

WDataValue Documentation Map

So whether you are updating a field with an int (SDWORD) or retrieving a string (WString) you will use WDataValue as the interface class. But how are bitmaps and other binary data stored and retrieved.

It amazes me how often this question is asked when there is no difference between binary data and any other data type in terms of updating a field.

Sample

Bitmap Database Application

I have the transaction object connected to a SQLAnywhere 5.0 Database that has one table in it - tbitmap. This table has columns of ID (int), a name (long varchar), and a bitmap (long binary).

I have bound the two labels to the ID and name columns and bound the PictureBox to the bitmap column. That is all that needs to be done to display a bitmap from a database. The DataNavigator will step through the database showing each bitmap in turn.

To add binary data to a data base, I first allowed the user to browse for a bmp file. Then I opened the file, determined its size, allocated a suitable buffer, read the file, and then created a WBuffer. Get the BMP file and build the Buffer

_bitmapBuffer is an instance variable of my class. Now with a WBuffer I can create a WDataValue. One very important point is how the WBuffer _bitmapBuffer calls the Create() function. The first FALSE means "do not copy a separate version of the data into the WBuffer, just use the reference" and the second FALSE means do not delete the original pointer to data after the WBuffer is created.

Now we create the WDataValue out of the WBuffer, determine which column should be updated, and call SetValue() on that column using the WDataValue. If we call the WQuery function Add(), then we will be adding a new row, otherwise we would be modifying an existing row.

Create the WDataValue and Update

Dynamic Index

I prefer to determine the column's index dynamically rather than code the number in, because the index number is based on the order of the result set. So the two query statements

'SELECT name, bitmap from DBA."tbitmap" tbitmap' and

'SELECT bitmap, name from DBA."tbitmap" tbitmap

will result in the bitmap column having two different indexes.

Work Done For You

SetValue(), GetValue() and the binding of a components all work with any size of data. You do not need to get 32K and then get more. The WQuery object does that for you.

Full Source

I have omitted many of the return code checks. The SetValue() calls should certainly be determined for success. For the full example, download the Power++ v1.6 project source here. Make sure that you create a directory for the project - BitmapDatabase is suggested - before you unzip it. You will need to set up a database with the three columns mentioned. Make sure that the types are INT, LONG VARCHAR, and LONG BINARY.

Also, you will need to modify the WQuery object to make sure that the Database has Read/Write access.



Click here to see Tip#12

Return to Power++ Home Page.


to top of page

Copyright © 1999 Sybase, Inc. All Rights Reserved.
Privacy Policy
Legal