Imprimir imagen guardada en tabla SQL

Imprimir imagen guardada en tabla SQL

Postby jpcavagnaro » Wed Dec 16, 2020 12:39 pm

Hola buen día, necesito imprimir una imagen guardada en una tabla sql, que la guarde así:

oRsFoto:Fields( "Foto" ):Value := STRTOHEX( MEMOREAD( aFiles[ i ] ) )

Estoy tratando de imprimirla así: ImprimirFoto( oRsFoto:Fields( "Foto" ):Value )

Function ImprimirFoto(wfoto)
PRINT oPrn PREVIEW MODAL
PAGE
oPrn:SayImage(0,0, wfoto, oPrn:nHorzRes(), oPrn:nVertRes() , nil, .t. )
ENDPAGE
ENDPRINT

retu .t.

No da error, pero muestra una hoja en blanco.

Saludos
Jorge
Jorge
Saludos.
jpcavagnaro
 
Posts: 155
Joined: Tue Oct 11, 2016 1:02 pm
Location: Luján, bs. as.

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Mon Dec 21, 2020 12:10 pm

This should work, if you created the table with FOTO as BLOB datatype.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Imprimir imagen guardada en tabla SQL

Postby Rick Lipkin » Tue Dec 22, 2020 2:00 pm

Rao

I am using MS Sql Server .. there is no "Blob Type" .. however VarBinary(max) works for me ..

Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Imprimir imagen guardada en tabla SQL

Postby jpcavagnaro » Wed Dec 23, 2020 10:21 pm

Hola la imagen está como VarBinary (max) y no funciona.

Lo solucione guardando la imagen en un temporal.jpg e imprimir desde ese temporal y funciona perfecto.

Saludos.
Jorge
Jorge
Saludos.
jpcavagnaro
 
Posts: 155
Joined: Tue Oct 11, 2016 1:02 pm
Location: Luján, bs. as.

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Thu Dec 24, 2020 4:03 pm

Can you please let us know if you are using MySql or MSSQL and also the version of FWH you are using?
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Fri Dec 25, 2020 2:27 pm

jpcavagnaro wrote:Hola la imagen está como VarBinary (max) y no funciona.

Lo solucione guardando la imagen en un temporal.jpg e imprimir desde ese temporal y funciona perfecto.

Saludos.
Jorge


Not necessary.
This is an example for MSSQL.
This sample uses MSSQL server in the cloud provided by FiveTech for demonstration purposes.
Please copy the sample to \fwh\samples folder and build with buildx.bat or buildh.bat

Code: Select all  Expand view  RUN
#include "fivewin.ch"

function Main()

   local oCn, nVer, cSql, oRs, oPrn

   oCn   := FW_OpenAdoConnection( { "MSSQL", "208.91.198.196", "gnraore3_", "fwhmsdemo", "fwh@2000#" }, .t. )
   if oCn == nil
      ? "Connect fail"
      return nil
   end
   nVer     := Val( oCn:Properties( "DBMS Version" ):Value )

   // CREATE TABLE
   TRY
      oCn:Execute( "DROP TABLE [adoimage]" )
   CATCH
   END

if nVer < 9
//
TEXT INTO cSql
CREATE TABLE [adoimage] (
   [ID] INT IDENTITY( 1, 1 ) PRIMARY KEY,
   [NAME] VARCHAR ( 10 ),
   [FOTO] IMAGE
)
ENDTEXT
//
else
//
TEXT INTO cSql
CREATE TABLE [adoimage] (
   [ID] INT IDENTITY( 1, 1 ) PRIMARY KEY,
   [NAME] VARCHAR ( 10 ),
   [FOTO] VARBINARY(max)
)
ENDTEXT
//
endif

   oCn:Execute( cSql )

   // ADD ONE RECORD TO THE TABLE
   oRs   := FW_OpenRecordSet( oCn, "adoimage" )
   oRs:AddNew()
   oRs:Fields( "NAME" ):Value := "OLGA1"
   oRs:Fields( "FOTO" ):Value := STRTOHEX( MEMOREAD( "c:\fwh\bitmaps\olga1.jpg" ) )
   oRs:Update()
   oRs:Close()

   // OPEN THE TABLE AND PRINT
   oRs   := FW_OpenRecordSet( oCn, "adoimage" )

   PRINT oPrn PREVIEW
   PAGE
   oPrn:SayImage(0,0, oRs:Fields( "FOTO" ):Value, 1800, 2400, nil, .t. )
   ENDPAGE
   ENDPRINT

   oRs:Close()
   oCn:Close()

return nil
 


Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Fri Dec 25, 2020 3:38 pm

Hola la imagen está como VarBinary (max) y no funciona.

This depends on the MSSQL server version.
For versions less than 9.0, we need to use
Code: Select all  Expand view  RUN

   [FOTO] IMAGE
 

and for later versions, we can use
Code: Select all  Expand view  RUN

   [FOTO] VARBINARY(max)
 


Please see the sample above.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Fri Dec 25, 2020 4:12 pm

We have seen that the SQL statement required to create a table can be different for different versions of MSSQL. Naturally it is different for different RDBMS like MSSQL, MYSQL, ORACLE, etc.

For example the SQL for the same table can be:
Code: Select all  Expand view  RUN

CREATE TABLE `adoimage` (  
   `ID` INT AUTO_INCREMENT PRIMARY KEY,
   `NAME` VARCHAR ( 10 ),
   `FOTO` LONGBLOB
)
 


For MSACCESS:
Code: Select all  Expand view  RUN

CREATE TABLE [adoimage] (  
  [ID] AUTOINCREMENT PRIMARY KEY,
  [NAME] VARCHAR ( 10 ),
  [FOTO] LONGBINARY
)
 


Instead of writing SQL statements for creation of tables on our own, we recommend using FWH function
Code: Select all  Expand view  RUN

FWAdoCreateTable( cTable, aStruct, oCn, [lAddAutoInc := .t.] )
 

aStruct is a normal DBF style structure array we are all very familiar with. Data type "M" creates a long text field and datatype "m" creates a long binary field.
This function internally creates an sql statement appropriate for the RDBMS and its version and we are relieved from the trouble of finding the correct datatype.

The next sample demonstrates using this function for MSSQL, MYSQL and MSACCESS. Using FWH provided ADO functions, we can write code that works with different RDBMS (also versions) without changing our application code:
Code: Select all  Expand view  RUN
#include "fivewin.ch"

function Main()

   local nCh, oCn, oRs, oPrn
   local cTable   := "adoimage"
   local aImages  := { "c:\fwh\bitmaps\olga1.jpg", "c:\fwh\bitmaps\sea.bmp" }

   nCh   := Alert( "RDMBS", { "MySQL", "MSSQL", "MSACCESS" }, "Select" )

   if nCh == 1
      oCn   := FW_OpenAdoConnection( { "MYSQL", "54.37.60.32", "fwh", "fwhuser", "fwh202006" }, .t. )
   elseif nCh == 2
      oCn   := FW_OpenAdoConnection( { "MSSQL", "208.91.198.196", "gnraore3_", "fwhmsdemo", "fwh@2000#" }, .t. )
   elseif nCh == 3
      oCn   := FW_OpenAdoConnection( "xbrtest.mdb" )
   else
      return nil
   endif

   if oCn == nil
      ? "Connection error"
      return nil
   endif

   TRY
      oCn:Execute( "DROP TABLE " + cTable )
   CATCH
   END

   FWAdoCreateTable( cTable, { { "NAME", "C", 10, 0 }, { "FOTO", "m", 8, 0 } }, oCn )

   oRs   := FW_OpenRecordSet( oCn, cTable )

   // Append one image
   oRs:AddNew()
   oRs:Fields( "NAME" ):Value := Upper( cFileNoExt( aImages[ 1 ] ) )
   oRs:Fields( "FOTO" ):Value := STRTOHEX( MEMOREAD( aImages[ 1 ] ) ) // Harbour: HB_STRTOHEX(...)
   oRs:Update()

   // Append second image. Simpler method
   oRs:AddNew( { "NAME", "FOTO" }, { Upper( cFileNoExt( aImages[ 2 ] ) ), HB_STRTOHEX( MEMOREAD( aImages[ 2 ] ) ) } )

   oRs:Close()

   oRs   := FW_OpenRecordSet( oCn, cTable )
   XBROWSER oRs
   oRs:MoveFirst()

   PRINT oPrn PREVIEW

   PAGE
   @ 1,1 PRINT TO oPrn IMAGE oRs:Fields( "FOTO" ):Value SIZE 6,4 INCHES
   oRs:MoveNext()
   @ 6,1 PRINT TO oPrn IMAGE oRs:Fields( "FOTO" ):Value SIZE 6,4 INCHES
   ENDPAGE

   ENDPRINT

   oRs:Close()

return nil


For all RDBMSs it is the same code and same result:
Image
Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Imprimir imagen guardada en tabla SQL

Postby richard-service » Sat Dec 26, 2020 4:58 am

nageswaragunupudi wrote:
Hola la imagen está como VarBinary (max) y no funciona.

This depends on the MSSQL server version.
For versions less than 9.0, we need to use
Code: Select all  Expand view  RUN

   [FOTO] IMAGE
 

and for later versions, we can use
Code: Select all  Expand view  RUN

   [FOTO] VARBINARY(max)
 


Please see the sample above.


Mr.Rao
MySQL v8.0.13 same as support it.
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 804
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: Imprimir imagen guardada en tabla SQL

Postby Rick Lipkin » Sat Dec 26, 2020 2:04 pm

Rao

I have looked for LONGBINARY in the field selections of Ms Access.mdb and .ACCDB and that data type is not an option .....

Image

I am using ole object a field to store pictures in Ms Access ....

Image

???

Thanks
Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2666
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Mon Dec 28, 2020 9:58 pm

richard-service wrote:
nageswaragunupudi wrote:
Hola la imagen está como VarBinary (max) y no funciona.

This depends on the MSSQL server version.
For versions less than 9.0, we need to use
Code: Select all  Expand view  RUN

   [FOTO] IMAGE
 

and for later versions, we can use
Code: Select all  Expand view  RUN

   [FOTO] VARBINARY(max)
 


Please see the sample above.


Mr.Rao
MySQL v8.0.13 same as support it.


The discussion about field types IMAGE and VARBINARY(max) are for MSSQL ( Micorosoft SQL Server ) but not for MySQL.
Because you are using MySQL, this does not apply to you.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Imprimir imagen guardada en tabla SQL

Postby nageswaragunupudi » Mon Dec 28, 2020 10:03 pm

Rick Lipkin wrote:Rao

I have looked for LONGBINARY in the field selections of Ms Access.mdb and .ACCDB and that data type is not an option .....

Image

I am using ole object a field to store pictures in Ms Access ....

Image

???

Thanks
Rick Lipkin


https://docs.microsoft.com/en-us/sql/od ... rver-ver15

You can choose OLE and then store any binary data in the field. But when we create the table using SQL, better we use the datatype VARBINARY or LONGBINARY.

Also see
https://codekabinett.com/rdumps.php?Lan ... s-database
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 108 guests