Sunday, September 2, 2012

How Import multiple images to SQL Server using SSIS


Sometimes we need to import thousands of images to SQL Server. This example shows how to import a list of images to SQL Server using SQL Server Integration Services.

Requirements

  • SQL Server Enterprise or Standard (in this case I am using SQL Server 2008 R2, but it can work with SQL Server 2005 as well).
  • SSIS installed (it is included in the SQL Server installer).

Demonstration

  1. First of all, it is necessary to create a flat file named listImages.txt (or the name of your preference) with the paths of the images that you want to import to SQL Server such as the following:




    C:\images\pic1.jpg
    C:\images\pic2.jpg
    C:\images\pic3.jpg
  2. Create a table myImages with 3 columns: The ID is the primary key, image will store the picture and the path will store the image path:
     
    CREATE TABLE [dbo].[myImages](
     [id] [smallint] IDENTITY(1,1) NOT NULL,
     [path] [varchar](200) NULL,
     [image] [image] NULL,
     CONSTRAINT [PK_myImages] PRIMARY KEY CLUSTERED 
    (
     [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
    IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
    ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
  3. Start the SQL Server Business Intelligence Studio and create a New SQL Server Integration Project.
     
    The Business Intelligence Development Studio
  4. Drag and drop the Data Flow Task to the design pane.
     
    Drag and drop the Data flow
  5. In the design pane double click the Data Flow Task
  6. In the Data Flow tab drag and drop a Flat File Source, an Import Column and an OLE DB Destination.  Join the tasks with the green arrows as shown below.
     
    Create a Flat file, an import column and a OLEDB Destination
  7. The Flat File Source will connect to the listImages.txt created in step 1. Double click on the Flat File Source to edit the settings.
  8. In the Flat file connection manager, press New... 
    Select the flat file
  9. In the Flat File Connection Manager Editor write a Connection manager name (any name can be used. In this example we will use imagefile).
  10. In the File name press Browse... and select the listImages.txt file created in step 1 (it can be stored anywhere. In this example it is in c:\images\)
     
    Define the flat file delimiters
  11. Select the Advanced options and in the Name, type Path to change the column name as shown below.
     
    change column name
  12. Then click OK on each window to save these settings.
  13. Next double click the Import Column transform and click the Input Columns tab as shown below.
  14. In the Input Columns tab check Name
     
    Check the name
  15. Click the Input and Output Properties
  16. Open the Import Column Output tree and select the Output Columns as shown below.
  17. Click the Add Column...
  18. button and name the new column Image
     
    Select the output column
  19. Get the ID property value of the column created (in this example the ID is 42).
     
    Verify the ID
  20. In the Input and Output Properties tab, open the Import Column Input > Input Columns and select Path as shown below.
  21. In the FileDataColumnID property write the ID from step 18 (in this example 42) and press OK to save these settings.
     
    Specify the ID
  22. Double click the OLE DB Destination.
  23. In the OLE DB Destination Editor Window, press New... for the OLE DB connection manager.
     
    Select the OLE BD Destination
  24. In the Configure OLE DB Connection Manager, press the New... button.
     
    Create the Data Connection
  25. In the Connection Manager, in the Provider combo box, select Native OLE DB\SQL Server Native Client.
  26. In the Server name write the SQL Server Name (in this example, the local server name is used which can be specified with a period).
  27. Select the Log on to the server information (in this example Windows Authentication is used).
  28. In the select or enter the database name, select the database used to create the table in step 2 (in this example, the Adventureworks database is used) and press OK.
     
    Select the server name and Database
  29. In the OLE DB Destination Editor name of the table or the view, select the table created in step 2 (in this example the table name is myImages).
     
    Select the table
  30. In the OLE DB Destination Editor, press the Mappings page and then press OK.
     
    Map the columns
  31. Now we are ready to start the project. Press the start debugging icon (green arrow). If everything is OK, the tasks should be colored green and the number of rows imported should be displayed.
     
    Run the package
  32. To verify that the data was imported successfully, open SQL Server Management Studio.
  33. Go to the database (in this example, Adventureworks) and open the myImages table.
     
    Verify the values

Next Steps

  • In this article, we imported multiple images at once using SQL Server Integration Services, see if you can follow these steps for your needs.
  • SSIS let's you import multiple images with the import column task, so keep this in mind the next time you need to import images.
  • Review the following tips and other resources:

No comments:

Post a Comment