LCLSX - Extract Embedded Image from SQL Database

Hi,

I am trying to gather employee information from a SQL system, including a photo which is held in an embedded field on the SQL form onto a notes document.

I have managed to gather the standard field information (text fields etc) using the code below but I am unsure how to extract the embedded image and add it to my notes document. The image is held in the field called LNL_BLOB.

I have read Andre’s sandbox entry on dealing with File Attachments but I have don’t have a very strong understanding of ODBC connectors as this is my first project using them.

Here is a version of my code :

Sub Initialize

Dim sqlConnection As New LCConnection("odbc2")	

Dim notesConnection As New LCConnection("notes") 

Dim sqlFieldList As LCFieldList

Dim notesFieldList As LCFieldList

Dim blobField As LCField

Dim count As Double	

Dim SQL_STATEMENT As String



Set lcSession = New LCSession	

SQL_STATEMENT = |

SELECT EMP.SSNO, EMP.ID, EMP.LASTNAME, EMP.FIRSTNAME

FROM AccessControl.dbo.EMP EMP

WHERE (EMP.SSNO>‘0’)

ORDER BY EMP.SSNO

|

SQL_STATEMENT = |

SELECT EMP.SSNO, EMP.ID, EMP.LASTNAME, EMP.FIRSTNAME, MMOBJS.LNL_BLOB, MMOBJS.LASTCHANGED

FROM AccessControl.dbo.EMP EMP, AccessControl.dbo.MMOBJS MMOBJS

WHERE EMP.ID = MMOBJS.EMPID AND ((EMP.SSNO>‘0’))

ORDER BY EMP.SSNO

|	

' -- Connect to SQL Database

sqlConnection.Userid = userid

sqlConnection.Password = password

sqlConnection.server = connection	

sqlConnection.metadata = table

sqlConnection.Connect



' -- Connect to Notes Database

notesConnection.server = servername

notesConnection.database = dbname

notesConnection.metadata = formname

notesConnection.writeback = True

Call notesConnection.Connect



'initalise the fieldlist

Set sqlFieldList = New LCFieldList(1, XREF_TRUNCATION_FLAGS)



'execute sql

Call sqlConnection.Execute(SQL_STATEMENT, sqlFieldList)



Set notesFieldList = New LCFieldList

Call notesFieldList.MapName (sqlFieldList, _

"SSNO, ID, LASTNAME, FIRSTNAME, LNL_BLOB, LASTCHANGED", _

"SSNO, ID, LASTNAME, FIRSTNAME, LNL_BLOB, LASTCHANGED" )



Do Until sqlConnection.fetch (sqlFieldList) = 0			

	Set blobField = sqlFieldList.Lookup("LNL_BLOB")	

	Call notesConnection.insert(notesFieldList)

Loop



sqlConnection.Disconnect

notesConnection.Disconnect

lcSession.ClearStatus

End Sub

Many thanks in advance

Fee

Subject: LCLSX - Extract Embedded Image from SQL Database

Fiona,

ODBC cannot handle image columns. You will have to rewrite your code in a Java agent and use JDBC to connect to MS SQL Server. We ran into this limitation 4 years ago when we tried to retrieve PDF files stored in an image column in MS SQL Server 2000.

Ken

Subject: RE: LCLSX - Extract Embedded Image from SQL Database

Thanks for your response Ken, much appreciated.

After much deliberation we have come up with the following solution, which basically uses the stream of the blob field and writes it to a new file.

Dim sqlConnection As New LCConnection(“odbc2”)

Dim sqlFieldList As LCFieldList

Dim blobField As New LCField (LCTYPE_BINARY)

Dim idField As LCField, lastNameField As LCField, firstNameField As LCField, lastChangedField As LCField

Dim blobStream As LCStream

Dim intFreeFile As Integer

Dim sqlStatement As String

Set lcSession = New LCSession

sqlStatement = |

SELECT EMP.LASTNAME, EMP.FIRSTNAME, BADGE.ID, MMOBJS.LNL_BLOB, MMOBJS.LASTCHANGED

FROM AccessControl.dbo.BADGE BADGE, AccessControl.dbo.EMP EMP, AccessControl.dbo.MMOBJS MMOBJS

WHERE EMP.ID = BADGE.EMPID AND EMP.ID = MMOBJS.EMPID AND ((MMOBJS.TYPE=2) AND (BADGE.STATUS=1))

ORDER BY EMP.LASTNAME

|

’ – Connect to SQL Database

sqlConnection.Userid = “HRConnect”

sqlConnection.Password = “DOMINO”

sqlConnection.server = “LENEL2”

sqlConnection.metadata = “AccessControl.dbo”

sqlConnection.Connect

'initalise the fieldlist

Set sqlFieldList = New LCFieldList(1, REF_TRUNCATION_FLAGS)

'execute sql

Call sqlConnection.Execute(sqlStatement, sqlFieldList)

Do Until sqlConnection.fetch(sqlFieldList) = 0

Set blobField = sqlFieldList.Lookup("LNL_BLOB")

Set idField  = sqlFieldList.Lookup("ID")

Set firstNameField = sqlFieldList.Lookup("FIRSTNAME")

Set lastNameField = sqlFieldList.Lookup("LASTNAME")

Set lastChangedField = sqlFieldList.Lookup("LASTCHANGED")

	

Set blobStream = blobField.GetStream(1, LCSTREAMFMT_BLOB)

If blobStream.Length <> 0 Then

	intFreefile = Freefile 'Create file to local disk for handling attachment data

	

	Open "C:\temp\photos\" & idField.Value(0) & SEP &  firstnameField.Value(0) & SEP & _

		lastNameField.Value(0) & ".jpg" For Binary As intFreefile			

		

	Put intFreefile, 1, blobStream.Value(0) 'Feed stream data into new binary-formatted file

	Close intFreefile 'Saves file to disk

End If

Loop

sqlConnection.Disconnect

lcSession.ClearStatus

'now you can attach your file to your Notes database as normal

I hope this can help other people in the future!

Fee

Subject: RE: LCLSX - Extract Embedded Image from SQL Database

Dear Fiona,do you have notice of “cut off” of the file when using the LCLSX to transfer ?

this is the “problem”

Set blobStream = blobField.GetStream(1, LCSTREAMFMT_BLOB)

the file result “cute off”…

in special mode the file is downloaded in the file system but when try to open it sometimes works and sometimes not work (file corruption)

do you have solved this problem ?

see also this Andre Guirard method to get files from BLOB:

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/259c72fa0097014885256f860000f826?OpenDocument

Subject: LCLSX - Extract Embedded Image from SQL Database

Pls help me to do the reverse procedure of inserting a file attachment by reading form file server and inserting it into an image data type in SQL server …Thnaks