Implementing a Database with the MS ADO Data Control (C++ Builder 4)

In Visual Basic, the LEADTOOLS OLEDB control is a OLEDB data bound control. This means you can associate the LEADTOOLS OLEDB control with an ADO data control so that the LEADTOOLS OLEDB control's bitmap is bound to a specified field in the data control's recordset.

Take the following steps to start a project and to add some code that associates the LEADTOOLS OLEDB control with an ADO data control:

1.

Start C++ Builder.

2.

On the Add-Ins menu, select the Visual Data Manager option.

3.

On the Data Manager window's Select File menu, select the New Database option.

4.

Specify a file name of leadpic.mdb and click the Save button.

5.

In the Database Manager window, click the New button.

6.

In the Add Table window, specify a table named: people, with the following two fields:

 

a.

Field name: who. Data Type: text. Size: 255

 

b.

Field name: photo. Data Type: Long Binary. Size: N/A.

7.

Close the Database Manager

8.

Add the LEAD RasterProcess Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD RasterProcess Object Library (14.5).

9.

Add the LEAD RasterIO Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD RasterIO Object Library (14.5).

10.

Add the following code to the general declarations section of your project.

Public RasterIO As LEADRasterIO
Public RasterProc As LEADRasterProcess

11.

Add the following code to the end of the Form_Load event handler.

'Create the RasterIO Object
Set RasterIO = CreateObject("LEADRasterIO.LEADRasterIO.145 ")
'Create the RasterProcess Object
Set RasterProc = CreateObject("LEADRasterProcess.LEADRasterProcess.145 ")

12.

On the Project pull-down menu, use the Components option to add the MS ADO data control to your project. Then add an ADO control to the bottom of your form and change its properties as follows:

 

a.

Set the ConnectionString property to connect to leadpic.mdb. (Use the browser to properly build the connection string using the OLEDB ODBC Provider.)

 

b.

Set the RecordSource by setting the CommandType to adCommandTable and the table to the "people" table.

 

c.

Set the Name property to Dpeople.

13.

On the Project pull-down menu, use the Components option to add the LEADRasterView Control module to your project.

14.

Select the LEADRasterView control; then add the control to your main form. Stretch and position the control as you want it to appear at run time.

15.

On the Project pull-down menu, use the Components option to add the LEADTOOLS Raster OLE DB Custom Control module to your project. Select the LEADTOOLS Raster OLE DB control; then add the control to your main form.

16.

In the Properties box for the LEADTOOLS Raster OLE DB control, make the following changes:

 

a.

Set the DataSource property to Dpeople.

 

b.

Leave the DataMember property empty, in order to use the default for this data source.

17.

Add a TextBox control to your form and change its properties as follows:

 

a.

Set the DataSource property to Dpeople.

 

b.

Set the Name property to Cwho.

18.

Add three command buttons to your form and name them as follows:

 

Name

Caption

 

AddPhoto

Add Photo

 

DeletePhoto

Delete Photo

 

FlipPhoto

Flip Photo

19.

On the Project pull-down menu, use the Components option to add the Microsoft Common Dialog Control module to your project.

20.

Add a Microsoft CommonDialog Control to your form.

21.

Add the following code to the form's Load procedure. In online help, you can use the Edit pull-down menu to copy the block of code.

'These are all persistent properties that can be set in the properties box.
LEADRasterView1.Appearance = APPEARANCE_FLAT
LEADRasterView1.BorderStyle = 1
LEADRasterView1.BackColor = RGB(255, 255, 125)
LEADRasterView1.PaintDither = PAINTDITHER_DIFFUSION
LEADRasterView1.PaintPalette = PAINTPALETTE_AUTO
LEADRasterView1.AutoRepaint = True
LEADRasterView1.AutoSize = False
LEADRasterView1.AutoSetRects = True
LEADRasterView1.PaintSizeMode = PAINTSIZEMODE_FIT

'databind performed at design-time except for datafield
LEADRasterOLEDB1.DataField = "photo"
Cwho.DataField = "who"

' Set the data properties on the LEAD Raster OLE DB control
LEADRasterOLEDB1.DataLoadBits = 0
LEADRasterOLEDB1.DataSaveBits = 24
LEADRasterOLEDB1.DataSaveFormat = FILE_LEAD
LEADRasterOLEDB1.DataSaveQuality = QFACTOR_QMS
LEADRasterOLEDB1.EnableMethodErrors = True

22.

Code the AddPhoto button's Click event as follows:

On Error GoTo ERRORHANDLER
'release any reference
LEADRasterOLEDB1.Raster.Bitmap = 0
CommonDialog1.Filter = "Grapics|*.cmp;*.jpg;*.jff;*.jtf;*.bmp;*.tif;*.tga;*.pcx;*.cal;*.mac;*.mac;*.img;*.msp;*.wpg;*.wpg;*.ras;*.pct;*.pcd;*.eps;*.wmf"
CommonDialog1.Flags = cdlOFNFileMustExist
CommonDialog1.DialogTitle = "Open File"
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
Myfile = CommonDialog1.FileName
If Myfile = "" Then Exit Sub

Dpeople.Recordset.AddNew
RasterIO.Load LEADRasterView1.Raster, Myfile, 0, 1, 1
LEADRasterOLEDB1.Raster = LEADRasterView1.Raster
LEADRasterOLEDB1.DataDirty = True
Cwho = Myfile

' Specify the appropriate properties for saving the image in the database
If LEADRasterView1.Raster.BitmapBits = 1 Then
LEADRasterOLEDB1.DataSaveBits = 1
LEADRasterOLEDB1.DataSaveFormat = FILE_LEAD1BIT
ElseIf LEADRasterView1.Raster.BitmapBits = 4 Then
LEADRasterOLEDB1.DataSaveBits = 4
LEADRasterOLEDB1.DataSaveFormat = FILE_PCX
ElseIf LEADRasterView1.Raster.IsGrayscale = GRAY_NO Then
LEADRasterOLEDB1.DataSaveBits = 24
LEADRasterOLEDB1.DataSaveFormat = FILE_CMP
LEADRasterOLEDB1.DataSaveQuality = QFACTOR_QMS
Else 'save as grayscale
LEADRasterOLEDB1.DataSaveBits = 8
LEADRasterOLEDB1.DataSaveFormat = FILE_CMP
LEADRasterOLEDB1.DataSaveQuality = QFACTOR_QMS
End If

Dpeople.Recordset.Update

' Requery the data control's recordset
Dpeople.Recordset.Requery
Exit Sub

ERRORHANDLER:
If Dpeople.Recordset.EditMode = dbEditAdd Then
Dpeople.Recordset.CancelUpdate
End If
If (Err.Number <> ERROR_DLG_CANCELED) Then
MsgBox Err.Source + " " + CStr(Err.Number) + Chr(13) + Err.Description
End If

23.

Code the DeletePhoto button's Click event as follows:

Dpeople.Recordset.Delete
Dpeople.Recordset.MoveFirst

24.

Code the FlipPhoto button's Click event as follows:

RasterProc.Flip LeadRasterView1.Raster
LEADRasterOLEDB1.Raster.Bitmap = 0
LEADRasterOLEDB1.Raster = LEADRasterView1.Raster
LEADRasterOLEDB1.DataDirty = True

25.

Add the following code to the LEAD Raster OLEDB control's DataLoaded event:

If (nStatus = 0) Then
LEADRasterView1.Raster.RefBitmap = True 'don't make a copy
LEADRasterView1.Raster.Bitmap = LEADRasterOLEDB1.Raster.Bitmap 'send the image to the LEADRasterView
LEADRasterView1.Raster.RefBitmap = False 'reset
ElseIf (nStatus = ERROR_FILENOTFOUND) Then 'if it's an empty record
LEADRasterView1.Raster.Bitmap = 0
ElseIf (nStatus <> ERROR_FILENOTFOUND) Then 'if it's not an empty record
LEADRasterView1.Raster.Bitmap = 0
MsgBox "Error loading from database!"
End If
LEADRasterView1.ForceRepaint
LEADRasterOLEDB1.DataDirty = False

26.

Add the following code to the LEAD Raster OLEDB control's DataSaved event:

LEADRasterOLEDB1.Raster.Bitmap = 0 'free the current reference
LEADRasterView1.Raster.Bitmap = 0 'free the current bitmap
If (nStatus <> 0) Then
LEADRasterView1.ForceRepaint
MsgBox "Error saving to database!"
End If

27.

Run your program to test it.