Importing Image from a non Navision table to a Navision blob field

code-dynamics

In SQL it is possible to define tables with a column, which is of type image. This field type is not known by Navision or by ADO and can thereby not be extracted like other fields.

There exists 2 ways to get this fields value.

1) One way is to stream the image to a file and then afterwards import it into the blob.



In this example I assume you already have the ADO Connection and thereby already got the ADORecordSet.


 div |  copy code |? 
ADOStream.Type := 1; //1 = Binary
ADOStream.Open;
ADOStream.Write(ADORecordSet.Fields.Item(FieldName).Value);
ADOStream.SaveToFile('c:\tmp.bmp',2); //2 = SaveCreateOverWrite
ReturnTable.Picture.IMPORT('c:\tmp.bmp');
ADOStream.Close;
CLEAR(ADOStream);
 
Where ADOStream is 'Microsoft ActiveX Data Objects 2.8 Library'.Stream
and ADORecordSet is 'Microsoft ActiveX Data Objects 2.8 Library'.Recordset



This method works for compressed and uncompressed Blob fields.



2) Another way is to move/insert the image into your table by using ADO RecordSet.



Create 2 connections, one to your image table (ADOConn) and one to the table in Navision (NAVconn) where you want the picture to be inserted. Then “transfer” the Value from one RecordSet to another and add it.

 div |  copy code |? 
FromRecSet := ADOConn.Execute('SELECT image FROM tableX','',0);
FromRecSet.MoveFirst;
 
//The following query is used to get a "blank" recordset, just like a INIT in Navision
MyQuery := 'SELECT [EntryNo], [Picture] FROM [MyTable] WHERE [EntryNo] = 0';
 
NewRecSet.Open(MyQuery,NAVconn,1,3,1);
NewRecSet.AddNew;
NewRecSet.Fields.Item('Picture').Value := FromRecSet.Fields.Item('image').Value;
NewRecSet.Update;
 



This method does not work for compressed Blob fields!



If you need to know more about ADO Connection or ADO RecordSet, then please read my previous posts on ADO.

You can leave a response, or trackback from your own site.

2 Responses to “Importing Image from a non Navision table to a Navision blob field”

  1. Daniel says:

    I read similar article also named Importing Image from a non Navision table to a Navision blob field, and it was completely different. Personally, I agree with you more, because this article makes a little bit more sense for me

  2. Martin Schlemmer says:

    Hallo Mr. Byllemos,
    I tried your code(First method). We have NAV 2009 SP1 Classic.
    It works perfect for uncompressed files.
    For compressed files, the files are corrupt.
    Can you please help me.
    Thanks
    Kind regards
    Martin Schlemmer

Leave a Reply


5 × four =