ODBC Example for C++ Builder

Take the following steps to create and use a simple ODBC database, containing only images. (For a more sophisticated approach, refer to Using ODBC to Access Image Data.)

Note:

The LEADTOOLS COM requires ODBC drivers that can properly handle long binary fields. Examples have been tested and will work with version 3.0 of the Microsoft ODBC Desktop Driver Pack. (This driver pack ships with Access 95 and works on 32-bit systems.) You can download drivers from the LEAD BBS, the CompuServe forum, or the Worldwide Web home page. The CompuServe forum is at GO LEADTECH. The web page is ftp://ftp.leadtools.com/pub/utils/ODBC32.zip. The file to download is ODBC32.ZIP.

 

1.

Make a copy of the project that you created in Loading and Displaying an Image; then load the copied project.

2.

In the Add-Ins menu, select the Data Manager...

3.

In the Data Manager Window's File menu, select New, and create an Access database named c:\lead\lead.mdb.

4.

Click the New button, and create a table with one field as follows:

 

Name:

ltvbpic

 

Field Name:

photo

 

Data Type:

long binary

5.

Add the following LEAD Raster objects to your project,

 

a. LEADRasterODBC Object Library.

 

 

For VB4: On the Tools pull-down menu, use the References option, and select the LEAD Raster ODBC Object Library (14.5).

 

 

For VB5: On the Project pull-down menu, use the References option, and select the LEAD Raster ODBC Object Library (14.5).

 

b. LEADRasterProcess Object Library.

 

 

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

 

 

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

6.

Add the following statment to your public section:

Public RasterODBC As New LEADRasterODBC

7.

Exit the Data Manager.

8.

Open the ODBC administrator and click the Add button.

9.

Select Microsoft Access driver and click the OK button.

10.

Enter the name LEADACCESS.

11.

Click the Select directory button and select the LEAD directory.

12.

Click the OK button; click the next OK button; then click the Close button.

13.

In C++ Builder, replace the code in the main form's init procedure as follows:

'Position and size the main form so that it takes up most of the screen.
Width = Screen.Width * 0.9
Height = Screen.Height * 0.8
Left = (Screen.Width - Width) / 2
Top = (Screen.Height - Height) / 2
' Set the position and size of the LEAD control.
' Allow for a border of 1/8 of the form size.
' The units of measure do not matter, since we are calculating proportions.
HeightAllowed = ScaleHeight * 3 / 4
WidthAllowed = ScaleWidth * 3 / 4
LEADRasterView1.Left = (ScaleWidth - WidthAllowed) / 2
LEADRasterView1.Top = (ScaleHeight - HeightAllowed) / 2
LEADRasterView1.Width = WidthAllowed
LEADRasterView1.Height = HeightAllowed
'Turn off automatic repainting and turn on scroll bars.
LEADRasterView1.AutoRepaint = False
LEADRasterView1.AutoScroll = True
'Set the image display size to match the LEADRasterView control
LEADRasterView1.SetDstRect 0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight
LEADRasterView1.SetDstClipRect 0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight
' Open the database.
RasterODBC.Raster = LEADRasterView1.Raster
DString = "ODBC;DSN=LEADACCESS"
RasterODBC.dbOpen DString, "ltvbpic", "photo", DB_OPENOPTIONS_NONE
RasterODBC.dbLoadBits = 24
RasterODBC.dbLockingMode = DB_LOCKINGMODE_OPTIMISTIC
LEADRasterView1.ForceRepaint
If RasterODBC.dbIsBOF = True And RasterODBC.dbIsEOF = True Then
msg = "The database is empty."
answer = MsgBox(msg, 0, "Notice")
NextRec.Enabled = False
PrevRec.Enabled = False
End If

14.

Add a common dialog control to your main form and name it Commdlg.

15.

At the top of your main form, add seven command buttons and name them as follows: Add, Update, Delete, FirstRec, NextRec, PrevRec, and LastRec.

16.

For the Add button, add the following code to the click procedure:

Dim RasterIO As New LEADRasterIO
If RasterODBC.dbCanAppend = False Then
msg = "You cannot add to this database."
answer = MsgBox(msg, 0, "Error")
Else
RasterODBC.dbAddNew
CommDlg.Filter = "All Files|*.*"
CommDlg.Flags = cdlOFNFileMustExist
CommDlg.DialogTitle = "Open File"
CommDlg.CancelError = True
CommDlg.ShowOpen
filename = CommDlg.filename
RasterIO.Load LEADRasterView1.Raster, filename, 0, 0, 1
RasterODBC.Raster = LEADRasterView1.Raster
nret = RasterODBC.dbUpdate(FILE_CMP, 0, QFACTOR_QMS)
If nret <> 0 Then
msg = "You cannot update this database."
answer = MsgBox(msg, 0, "Error")
Else
RasterODBC.dbMoveLast
LEADRasterView1.ForceRepaint
msg = "Image added to the database."
answer = MsgBox(msg, 0, "LEAD")
NextRec.Enabled = True
PrevRec.Enabled = True
If RasterODBC.dbIsBOF = True Then
PrevRec.Enabled = False
Else
PrevRec.Enabled = True
End If
If RasterODBC.dbIsEOF = True Then
NextRec.Enabled = False
Else
NextRec.Enabled = True
End If
End If
End If

17.

For the Update button, add the following code to the click procedure:

Dim RasterProc As New LEADRasterProcess
If RasterODBC.dbCanUpdate = False Then
msg = "You cannot update this database."
answer = MsgBox(msg, 0, "Error")
ElseIf RasterODBC.dbIsBOF = True Or RasterODBC.dbIsEOF = True Then
msg = "There is no current record."
answer = MsgBox(msg, 0, "Error")
Else
If RasterODBC.dbEditMode = DB_EDITMODE_NONE Then
RasterODBC.dbEdit
RasterProc.Flip LEADRasterView1.Raster
LEADRasterView1.ForceRepaint
End If
RasterODBC.dbUpdate FILE_CMP, 0, QFACTOR_QMS
End If

18.

For the Delete button, add the following code to the click procedure:

If RasterODBC.dbCanUpdate = False Then
msg = "You cannot update this database."
answer = MsgBox(msg, 0, "Error")
ElseIf RasterODBC.dbIsBOF = True Or RasterODBC.dbIsEOF = True Then
msg = "There is no current record."
answer = MsgBox(msg, 0, "Error")
Else
RasterODBC.dbDelete
RasterODBC.dbRequery
RasterODBC.dbMoveFirst
If RasterODBC.dbIsBOF = True And RasterODBC.dbIsEOF = True Then
NextRec.Enabled = False
PrevRec.Enabled = False
End If
LEADRasterView1.ForceRepaint
End If

19.

For the First button, add the following code to the click procedure:

If RasterODBC.dbIsBOF = True And RasterODBC.dbIsEOF = True Then
msg = "The database is empty."
answer = MsgBox(msg, 0, "Notice")
NextRec.Enabled = False
PrevRec.Enabled = False
Else
RasterODBC.dbMoveFirst
PrevRec.Enabled = False
NextRec.Enabled = True
LEADRasterView1.ForceRepaint
End If

20.

For the Next button, add the following code to the click procedure:

If RasterODBC.dbIsBOF = True And RasterODBC.dbIsEOF = True Then
PrevRec.Enabled = False
NextRec.Enabled = False
msg = "The database is empty."
answer = MsgBox(msg, 0, "Notice")
Else
RasterODBC.dbMoveNext
If RasterODBC.dbIsEOF = True Then
NextRec.Enabled = False
RasterODBC.dbMovePrev
msg = "Already at last record."
answer = MsgBox(msg, 0, "Notice")
Else
PrevRec.Enabled = True
End If
LEADRasterView1.ForceRepaint
End If

21.

For the Prev button, add the following code to the click procedure:

If RasterODBC.dbIsBOF = True And RasterODBC.dbIsEOF = True Then
PrevRec.Enabled = False
NextRec.Enabled = False
msg = "The database is empty."
answer = MsgBox(msg, 0, "Notice")
Else
RasterODBC.dbMovePrev
If RasterODBC.dbIsBOF = True Then
PrevRec.Enabled = False
RasterODBC.dbMoveNext
msg = "Already at first record."
answer = MsgBox(msg, 0, "Notice")
Else
NextRec.Enabled = True
End If
LEADRasterView1.ForceRepaint
End If

22.

For the Last button, add the following code to the click procedure:

If RasterODBC.dbIsBOF = True And RasterODBC.dbIsEOF = True Then
msg = "The database is empty."
answer = MsgBox(msg, 0, "Notice")
NextRec.Enabled = False
PrevRec.Enabled = False
Else
RasterODBC.dbMoveLast
NextRec.Enabled = False
PrevRec.Enabled = True
LEADRasterView1.ForceRepaint
End If

23.

Update the main form's unload procedure as follows:

Private Sub Form_Unload(Cancel As Integer)
RasterODBC.dbClose
End Sub

24.

Run your program to test it.