Trying to read an Image from SQL

LarryLat
Member Posts: 3
I have developed a third party integration that pushes data out to SQL staging tables and pulls data from staging tables.
One of the tables I am trying to read from contains a Customer Signature. It is stored as an Image Datatype within SQL and seems to be identical to the way Navision stores BLOB files within Sql.
The third party provider assures me that the file is stored as a Bitmap.
I am trying to pull this data from SQL and store it into a BLOB file in a Navision staging table. I open up the connection to the SQL database, but I cannot seem to figure out how to grab that piece of data. All of my other routines work correctly (grabbing fields with types of Code, Text, Decimal, etc.), but I cannot grab the Image withour receiveing an error.
Any suggestions?
Thanks,
Larry
One of the tables I am trying to read from contains a Customer Signature. It is stored as an Image Datatype within SQL and seems to be identical to the way Navision stores BLOB files within Sql.
The third party provider assures me that the file is stored as a Bitmap.
I am trying to pull this data from SQL and store it into a BLOB file in a Navision staging table. I open up the connection to the SQL database, but I cannot seem to figure out how to grab that piece of data. All of my other routines work correctly (grabbing fields with types of Code, Text, Decimal, etc.), but I cannot grab the Image withour receiveing an error.
Any suggestions?
Thanks,
Larry
0
Comments
-
Blob must be transfered as files or some form of stream... Because you never knows the size (e.g. one image can have 2TB of data).0
-
So any assistance on how to do the Streaming or the file transfer?0
-
No, I never did that. It was just a hint in which direction you need to do your "research"...0
-
[Topic moved from Navision forum to SQL General forum]Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!0 -
Hi !
You can use ADO RecordSet to solve this.
I assume, that you already are using ADO to read records from the mentioned table. If Yes, then you can use ADO RecordSet to read the image.
There are 2 ways - as already mentioned by kine.
1) Stream the image to a file and then afterwards import it into the blob.
The Stream can be done by using ADO Stream.Simplified Stream Example: ADOStream.Open; ADOStream.WriteText(ADORecordSet.Fields.Item(FieldName).Value); ADOStream.Position:= 0; ADOStream.Type := 1; ADOStream.SaveToFile('c:\image.bmp',2); ADOStream.Close; CLEAR(ADOStream);
I guess you know how to import a file into a blob ;-)
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.Example: 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;
Hope this helps :-)0 -
Will the second way work if the NAV Blob field is compressed?0
-
FYI: This image datatype is going away. I'm not sure if it's still supported in SQL 2008.There are no bugs - only undocumented features.0
-
The second way will not work for NAV compressed BLOB fields. It only works for uncompressed fields.
If you try it - your code will not fail. Everything will look ok - but when you try to export /read your blob field it will get an error.
Therefore in case of compressed fields, I would use the first way.
Btw. I can see, that I have posted a wrong code to the first way... so here is the correct 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);
It is important that the Type is set at first or else you will not be able to read the image as a binary file. If Type is set after Open, you will read the image as Text - and this will not always work.0 -
thanks bylle for great answer. I helps me a lot in working with ado stream and blob
For one who want to read more about ado stream :
http://msdn.microsoft.com/en-us/library ... 32(v=vs.85).aspx0 -
Trying to test this with version 2013R2 and up. For testing purposes, I'm trying to extract from a NAV DB but I'm not able to view the file after it's copied into a physical one. The size is also smaller than the initial one that I've uploaded on the item card.0
-
That is because NAV compresses the data in a blob by default. The BLOB fields has a property "Compression" which is default Yes. Change it to No and try again.Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!1 -
In regards to compression if somebody still needs it,
save image to file using bcp where dbname is nav database and objexp is a table with one image column - for the export templatebcp "[dbname].[dbo].[objexp]" format nul -T -n -f C:\temp\testblob.fmt -S localhost\navdemo bcp "SELECT top 1 [User Code] from [dbname].[dbo].[Object Metadata]" QUERYOUT C:\temp\obj.zip -T -f C:\temp\testblob.fmt -S localhost\navdemo
remove first 8 bytes which are probably the size
deflate the stream like this (powershell code):$i = [System.IO.File]::OpenRead("C:\temp\obj.zip") $o = [System.IO.File]::OpenWrite("C:\temp\obj.txt") $t = New-Object System.IO.Compression.DeflateStream($i, [System.IO.Compression.CompressionMode]::Decompress) $t.CopyTo($o) $t.Close() $o.Close() $i.Close()
since this is all supported in .net, can be done fully in c#0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions