Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Crystal Reports Insert Images into a Database for use with Crystal Reports Overview This document describes methods of inserting images into several of the most common databases off which Crystal Reports can report. Upon completion, you will be familiar with how to insert images into databases and how to report off the image data using Crystal Reports (CR). The images described are supported in Crystal Reports 8 and later. New in Crystal Reports XI Dynamic Image Location: In Crystal Reports XI, pictures and graphics can now be placed in a report through a link in the database so that it is no longer necessary to store images within the database. This supports the common practice of storing images on the web server and storing references to those images in the database. For more information on Dynamic Image Location in CR XI refer to the Crystal Reports Online Help by pressing the 'F1' key. DISCLAIMER Because there are potentially several versions of the third-party products discussed in this document, the instructions provided may be different on your computer. Consult your product documentation to complete the instructions. Contents NEW IN CRYSTAL REPORTS XI..................................................................... 1 Dynamic Image Location: ............................................................................1 INTRODUCTION ............................................................................................ 3 BEFORE YOU BEGIN.................................................................................... 3 MICROSOFT ACCESS ................................................................................... 3 OLE Object Fields........................................................................................3 MICROSOFT SQL SERVER ........................................................................... 6 Image Fields .................................................................................................6 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. Page 1 Crystal Reports Insert Images into a Database for use with Crystal Reports ORACLE ..................................................................................................... 7 BLOB Fields.................................................................................................7 Long Raw Fields ..........................................................................................9 SYBASE ...................................................................................................... 9 Image Fields .................................................................................................9 DB2 ......................................................................................................... 11 BLOB Fields...............................................................................................11 INFORMIX .................................................................................................. 12 Byte Fields .................................................................................................12 BLOB Fields...............................................................................................12 PERVASIVE ............................................................................................... 14 Longvarbinary Fields.................................................................................14 PARADOX ................................................................................................. 14 Binary Fields..............................................................................................14 Graphic Field .............................................................................................14 LOTUS NOTES ........................................................................................... 15 FINDING MORE INFORMATION .................................................................... 15 CONTACTING BUSINESS OBJECTS FOR TECHNICAL SUPPORT ..................... 15 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 2 Crystal Reports Insert Images into a Database for use with Crystal Reports Introduction Crystal Reports 8 and later allows you to incorporate images into a report. An effective method to include images is to report off a field that contains the image data. This method is dependent on the following: The database type The data type of the field that contains the physical image data This document provides instructions based on the database type that you are currently using and the data types that are available. Before You Begin Before you begin, the following conditions must exist to successfully report off images in Crystal Reports: Ensure that an OLE server for the inserted image type is installed on the computer where the image will be inserted. To verify this, ensure that the image appears correctly in an application when doubleclicking on the image file in Windows Explorer. Ensure that you have been assigned adequate database permissions and rights. Microsoft Access OLE Object Fields The Access data type that is capable of containing images is OLE Object. Images are inserted, via OLE (Object Linking and Embedding), and are embedded into the database. In Access, complete the following steps to create an OLE Object field and to insert an image data into this field. 1. Create a table and add a field that is defined as OLE Object data type. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 3 Crystal Reports Insert Images into a Database for use with Crystal Reports 2. When the table is open for inserting data in the Datasheet View, right-click on the data cell and select Insert Object. The Insert Object dialog box appears. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 4 Crystal Reports Insert Images into a Database for use with Crystal Reports 3. Select the Create from File radio button. Browse for or enter the full path and name of the image file. This should appear in the File text box. NOTE Only select the Link check box if the image file is updated periodically. These changes to the image file would be reflected in the database. However, leaving the Link check box unselected will embed the image into the database and will no longer be dependent on the existence of the external image file. 4. Click OK and the image will be inserted. The image is represented only by text describing the image’s associated application in the data cell. Crystal Reports 8 and later can now report off image data that has been inserted into the Access OLE Object field. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 5 Crystal Reports Insert Images into a Database for use with Crystal Reports Microsoft SQL Server Image Fields The SQL Server Image data type is capable of containing image data. Image data is inserted into Image fields through the use of a Microsoft utility called BCP. BCP is a command line application that is installed as part of the SQL Server client installation. CAUTION Ensure that the SQL Server \bin folder is in the Windows search path. In SQL Server, complete the following steps to create a table with an Image field and to insert image data into this field. 1. Create a table and define a field as Image data type. CAUTION The table that is to contain the image data needs to be created in advance. It should contain only one field, with the field defined as type Image. Syntax of command line: CREATE TABLE yourtablename (yourfieldname image) 2. Using the BCP utility, enter the command line syntax as shown below. To insert image data, the syntax has been streamlined. C:\> BCP <database>.<owner>.<table_name> in <path>\<filename> -S<server_name> -U<username> -P<password> Where: <database> is the SQL Server database referenced, <owner> is the owner of the table, <table_name> is the name of the table, <path> is the fully qualified folder that the image file resides in, <filename> is the name of the image file, with extension, <server_name> is the name of the SQL server, <username> is the name of a valid user with INSERT rights on the table, <password> is the user’s password. For example: C:\> BCP Techtest.vantech.ImageTable in C:\Images\Xtreme.jpg –Sdbconn1 –Uvantech –Pvantech When running the BCP utility, you will be prompted for several parameter values. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 6 Crystal Reports Insert Images into a Database for use with Crystal Reports 1. Hit ENTER for the file storage type prompt. 2. Enter 0 for the prefix-length prompt. 3. Enter the length (size) of image file in bytes for the length of field prompt. NOTE The length (size) of the field can be determined by reviewing the Properties of the file in Windows Explorer. 4. Hit ENTER for field terminator prompt. 5. Enter n for save format information prompt. Crystal Reports 8 and later can now report off image data that has been inserted into the SQL Server Image field. Oracle Oracle has two data types that are capable of containing data: BLOB and Long Raw. BLOB Fields The Oracle BLOB data type is capable of containing image data. Image data is inserted into an Oracle BLOB field byte by byte. In Oracle, complete the following steps to create a directory on the Oracle server and to insert image data into the Oracle BLOB field byte by byte. 1. In the following PL/SQL code, create a directory on the Oracle server to contain the image file. BFILE fields are Oracle fields used to specify images stored in operating system files (for example: Xtreme.jpg) Syntax of PL/SQL code: ----------- NOTE: Text enclosed in angle brackets <**> represent text requiring customization by the user --------------------CREATE OR REPLACE DIRECTORY <TechID>_BLOBDATA AS '<path>'; -- A directory "object" is created, with <path> being the location of the image file(s) GRANT READ ON DIRECTORY <TechID>_BLOBDATA TO PUBLIC; /* The user running the CREATE TABLE and CREATE PROCEDURE scripts must have this grant. This grant is granted to this user from a user with the DBA role */ CREATE TABLE <TechID>_BLOBTABLE (BLOBID INTEGER, BLOBFORMAT VARCHAR2(3), BLOBLENGTH INTEGER, BLOBNAME VARCHAR2(50), BFILEFIELD BFILE, BLOBFIELD BLOB); -- The table that will contain the image(s) is created CREATE OR REPLACE PROCEDURE <TechID>_INSERTBLOBFROMBFILE ( P_BLOBNAME IN <TechID>_BLOBTABLE.BLOBNAME%TYPE ) /* When executed, this stored procedure will insert the image into 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 7 Crystal Reports Insert Images into a Database for use with Crystal Reports the BLOB field in the above table. The image is inserted into the table piece by piece. pieces of the image are inserted, plus the last remaining bytes that are left over */ 1 kb AS P_BLOBID P_BLOBFORMAT P_BLOBLENGTH P_BFILEFIELD P_BLOBFIELD BUFFER BUFFER_LENGTH OFFSET CURRENT_WRITE NUMBER_OF_WRITES required REMAINING_BYTES INTEGER; VARCHAR2(3); INTEGER; BFILE; BLOB; RAW(1024); INTEGER := 1024; INTEGER := 1; INTEGER; INTEGER; ----------- Unique row identifier File extension Length of file (in bytes) BFILE locator BLOB locator Read/Write Buffer Length of Buffer File offset indicator Counter Number of 1K writes INTEGER; -- Number of remaining bytes BEGIN SELECT (COUNT(BLOBID)+1) INTO P_BLOBID FROM <TechID>_BLOBTABLE; -- Retrieves the next unique row identifier P_BLOBFORMAT := UPPER(SUBSTR(P_BLOBNAME,(LENGTH(P_BLOBNAME)2),3)); -- Retrieves the file extension from file name P_BFILEFIELD := BFILENAME('<TechID>_BLOBDATA',P_BLOBNAME); -- Initializes the local BFILE locator to DIRECTORY object and file name -- DIRECTORY object, <TechID>_BLOBDATA, is a logical alias for physical directory on server's file system -- BFILE locator holds the path to the file name and is used to populate the BLOB locator P_BLOBLENGTH := DBMS_LOB.GETLENGTH(P_BFILEFIELD); -- Retrieves the length of file (in bytes) INSERT INTO <TechID>_BLOBTABLE VALUES (P_BLOBID,P_BLOBFORMAT,P_BLOBLENGTH,UPPER(P_BLOBNAME),P_BFILEFIELD, RAWTOHEX('0x00')); -- Inserts unique row identifier, file extension, length of file (in bytes), file name, BFILE locator and initialize BLOB locator SELECT BLOBFIELD INTO P_BLOBFIELD FROM <TechID>_BLOBTABLE WHERE BLOBID = P_BLOBID FOR UPDATE; -- Selects BLOB locator from table into local BLOB locator for updating DBMS_LOB.FILEOPEN(P_BFILEFIELD,DBMS_LOB.FILE_READONLY); -- Opens the file in read only mode NUMBER_OF_WRITES := TRUNC(P_BLOBLENGTH/BUFFER_LENGTH); -- Calculates the number of 1K writes required FOR CURRENT_WRITE IN 1..NUMBER_OF_WRITES LOOP -- This FOR loop loops until number of 1K writes is exceeded DBMS_LOB.READ(P_BFILEFIELD,BUFFER_LENGTH,OFFSET,BUFFER); -- Reads 1K bytes from local BFILE locator DBMS_LOB.WRITE(P_BLOBFIELD,BUFFER_LENGTH,OFFSET,BUFFER); -- Writes 1K bytes to local BLOB locator OFFSET := OFFSET + BUFFER_LENGTH; -- Increments the file offset by 1K bytes END LOOP; REMAINING_BYTES := P_BLOBLENGTH(BUFFER_LENGTH*NUMBER_OF_WRITES); 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 8 Crystal Reports Insert Images into a Database for use with Crystal Reports -- Calculates the remaining bytes IF (REMAINING_BYTES > 0) THEN -- This IF condition checks for remaining bytes DBMS_LOB.READ(P_BFILEFIELD,REMAINING_BYTES,OFFSET,BUFFER); -- Reads remaining bytes from local BFILE locator DBMS_LOB.WRITE(P_BLOBFIELD,REMAINING_BYTES,OFFSET,BUFFER); -- Writes remaining bytes to local BLOB locator END IF; DBMS_LOB.FILECLOSE(P_BFILEFIELD); -- Closes the file COMMIT; -- Commits the row to the database END; / BEGIN <TechID>_INSERTBLOBFROMBFILE('badimage1.tif'); END; -- This code executes the stored procedure to insert the image badimage1.tif / Crystal Reports 8 and later can now report off image data that has been inserted into the Oracle BLOB field. Long Raw Fields Images are inserted into Long Raw fields in Oracle using ADO through an application, such as Visual Basic. The image is separated into one Megabyte chunks and passed to an array as an element. The ADO method Appendchunk is then used to piece the arrayed chunks into the Long Raw field. Crystal Reports 8 and later can now report off image data that has been inserted into the Oracle Long Raw field Sybase Image Fields The Sybase Image data type is capable of containing image data. Image data can be inserted into Image fields through the use a Microsoft utility called BCP. BCP is a command line application that is installed as part of the Sybase client installation. CAUTION Ensure that the Sybase \bin folder is in the Windows search path. In Sybase, complete the following steps to create a table with an Image field and to insert image data into this field. 1. Create a table and define a field as Image data type. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 9 Crystal Reports Insert Images into a Database for use with Crystal Reports CAUTION The table that is to contain the image needs to be created in advance. It should contain only one field, with the field defined as type image. Syntax of command line: CREATE TABLE yourtablename (yourfieldname image) 2. Using the BCP utility, enter the command line syntax as shown below. To insert image data, the syntax has been streamlined. C:\> BCP <database>.<owner>.<table_name> in <path>\<filename> -S<server_name> -U<username> -P<password> Where: <database> is the Sybase database referenced, <owner> is the owner of the table, <table_name> is the name of the table, <path> is the fully qualified folder that the image file resides in, <filename> is the name of the image file, with extension, <server_name> is the name of the SQL server, <username> is the name of a valid user with INSERT rights on the table, <password> is the user’s password. For example: C:\> BCP Techtest.vantech.ImageTable in C:\Images\Xtreme.jpg –Sdbconn1 –Uvantech –Pvantech When running the BCP utility, you will be prompted for several parameter values. 1. Hit ENTER for file storage type prompt. 2. Enter 0 for prefix-length prompt. 3. Enter the length (size) of image file in bytes for length of field prompt. NOTE The length (size) of the field can be determined by checking the Properties of the file in Windows Explorer. 4. Hit ENTER for field terminator prompt. 5. Enter n for save format information prompt. Crystal Reports 8 and later can now report off image data that has been inserted into a Sybase Image field. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 10 Crystal Reports Insert Images into a Database for use with Crystal Reports DB2 BLOB Fields The DB2 data type that is capable of containing image data is BLOB. Image data is inserted into BLOB fields. In DB2, complete the following steps to create a table with a BLOB field and to insert image data into this field. 1. Create a table and define a field as BLOB. SQL Syntax for DB2 Command Center: CREATE TABLE <yourtablename> (BLOBID INTEGER, BLOBFORMAT VARCHAR(3), BLOBLENGTH INTEGER, BLOBNAME VARCHAR(50), BLOBFIELD BLOB(100K)); Where: <yourtablename> is the name assigned to the table, BLOBID contains an ID value, BLOBFORMAT contains the image format (for example: JPG), BLOBLENGTH is the size of the image file, in bytes, BLOBNAME is the name of the image file, BLOBFIELD is the actual image, referenced from the location and filename (for example: C:\Images\Image1.jpg) 2. Insert data, including the image file, into the table from a DEL file. DEL files are DB2 data files that contain comma-separated text. DEL files can be edited with an ASCII text editor, such as MS Notepad. Data in the DEL file must match the structure defined for it in the table that contains the data. Here are the contents of a DEL file that matches the table definition, defined above. JPG, 34015, Image1.jpg, C:\Images\Image1.jpg BMP, 399062, Image1.bmp, C:\Images\Image1.bmp TIF, 399168, Image1.tif, C:\Images\Image1.tif GIF, 53033, Image1.gif, C:\Images\Image1.gif PNG, 229382, Image1.png, C:\Images\Image1.png To insert the records, including the images, into the table, the following IMPORT command needs to be run by DB2 Command Center: IMPORT FROM <path>\<yourfilename>.DEL OF DEL MODIFIED BY LOBSINFILE INSERT INTO <table_name>; Where: <path> is the Windows path to the DEL file <yourfilename> is the name of the DEL file 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 11 Crystal Reports Insert Images into a Database for use with Crystal Reports <yourtablename> is the name of the table that was created with the CREATE TABLE command. Crystal Reports 8 and later can now report off image data that has been inserted into the DB2 BLOB-type field. Informix Informix has two data types capable of containing image data: Byte and BLOB. Byte Fields Informix Byte data type is capable of containing image data. NOTE The process to insert images into Informix Byte fields is identical to the process of inserting images into Oracle Long Raw fields. Images are inserted into Byte fields in Informix using ADO through an application, such as Visual Basic. The image is separated into one Megabyte chunks and fed into an array. The ADO method Appendchunk is then used to piece the arrayed chunks into the Byte field. Crystal Reports 8 and later can now report off image data that has been inserted into the Informix Byte field. BLOB Fields Informix BLOB data type is capable of containing image data. Before image data can be inserted into a BLOB field, a blobspace must be created on the Informix server. The blobspace is used to store BLOB-type data, including images. Once the blobspace has been created, the image is inserted into a table using the FILETOBLOB() function. The FILETOBLOB function is used within an SQL INSERT statement. In Informix, complete the following steps to create a blobspace, to create a table with a BLOB field and to insert the image data into this field. 1. Create the blobspace from the Informix server command prompt. This procedure must be done on the actual server computer. The Informix server command prompt item can be identified by a shortcut with the server’s name in the Informix Dynamic Server program group. For example, if the Informix server’s name is YourServer, the shortcut YourServer will appear in the Informix Dynamic Server program group. At the command prompt, create the blobspace with the following command: 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 12 Crystal Reports Insert Images into a Database for use with Crystal Reports onspaces -c -b <BLOB_Space_Name> -g 2 -p <path&filename> -o 0 -s 2000 Where: <BLOB_Space_Name> is any name assigned to the blobspace <path&filename> is the filename with its location (for example: C:\Images\MyImage.jpg) NOTE Use the specified values in the onspaces command line. Once the onspace command has been constructed, hit ENTER to create the blobspace. 2. In the Informix SQL Editor, create a table that has a field defined as a BLOB data type. For example: Create table ImageTest (ID int, YourImage BLOB) 3. Insert the image data into the table through a standard SQL INSERT statement. The image is inserted in the INSERT statement via the FILETOBLOB() function. FILETOBLOB() contains the following syntax: FILETOBLOB(‘<path>\<filename>.<image_extension>’, ‘<location>’) Where: <path> is the path to the image file <filename> is the name of the image file <image_extension> is the extension of the image file (eg. JPG) <location> is the computer where the insertion is occurring. If the image is being inserted on the server computer, use the value server. If the image is being inserted from a client workstation computer, use the value client. For example: INSERT INTO ImageTest VALUES (1, FILETOBLOB(‘C:\Images\MyImage.jpg’, ‘client’)) Crystal Reports 8 and later can now report off image data that has been inserted into an Informix BLOB field. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 13 Crystal Reports Insert Images into a Database for use with Crystal Reports Pervasive Longvarbinary Fields Pervasive Longvarbinary data type is capable of containing image data. Image data is inserted into Longvarbinary fields using ADO through an application such as Microsoft Visual Basic. The image is separated into one Megabyte chunks and passed into an array as elements. The ADO method Appendchunk is then used to piece the arrayed chunks into the Longvarbinary field. Crystal Reports 8 and later can now report off image data that has been inserted into the Pervasive Longvarbinary field. Paradox Paradox has three data types capable of containing image data: Binary, Graphic or OLE. However, JPEG files contained in an OLE field in Paradox do not display in Crystal Reports. This occurs with both native and ODBC connections. This is a known issue and has been assigned track ID 31823. To report off JPEG files in Paradox, insert the images into a Binary or Graphic field. Binary Fields Paradox Binary data type is capable of containing image data. Complete the following steps in Paradox: 1. Select the Binary field. 2. From the Edit menu and click Paste. 3. Select the JPEG file. Crystal Reports 8 and later can now report off image data that has been inserted into the Paradox Binary field. Graphic Field Paradox Graphic data type is capable of containing image data. Complete the following steps in Paradox: 4. Select the Graphic field. 5. From the Edit menu and click Paste. 6. Select the JPEG file. Crystal Reports 8 and later can now report off image data that has been inserted into the Paradox Graphic field. 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 14 Crystal Reports Insert Images into a Database for use with Crystal Reports Lotus Notes At the time of this writing, Lotus Notes was not able to define a field as an image data type such as BLOB, OLE Object, Graphic, Binary, Image. Lotus Notes is able to display JPEG files in fields defined as Rich Text Format (RTF). However, Crystal Reports does not support reporting off JPEG files that are contained in a Rich Text Format field. Finding More Information For more information, consult the product documentation of your database. For more information about new features in Crystal Reports XI, visit our website at http://www.businessobjects.com/products/reporting/crystalreports/ whatsnew.asp Contacting Business Objects for Technical Support We recommend that you refer to the product documentation and that you visit our Technical Support web site for more resources. Self-serve Support: http://support.Businessobjects.com/ Email Support: http://support.Businessobjects.com/support/answers.asp Telephone Support: http://www.Businessobjects.com/contact/support.asp 3/2/2005 9:46:00 AM Copyright © 2005 Business Objects. All rights reserved. cr_insert_images.pdf Page 15