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.
- 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).
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
- 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
- Start the SQL Server Business Intelligence Studio and create a New SQL Server Integration Project.
- Drag and drop the Data Flow Task to the design pane.
- In the design pane double click the Data Flow Task
- 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.
- 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.
- In the Flat file connection manager, press New...
- In the Flat File Connection Manager Editor write a Connection manager name (any name can be used. In this example we will use imagefile).
- 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\)
- Select the Advanced options and in the Name, type Path to change the column name as shown below.
- Then click OK on each window to save these settings.
- Next double click the Import Column transform and click the Input Columns tab as shown below.
- In the Input Columns tab check Name
- Click the Input and Output Properties
- Open the Import Column Output tree and select the Output Columns as shown below.
- Click the Add Column... button and name the new column Image
- Get the ID property value of the column created (in this example the ID is 42).
- In the Input and Output Properties tab, open the Import Column Input > Input Columns and select Path as shown below.
- In the FileDataColumnID property write the ID from step 18 (in this example 42) and press OK to save these settings.
- Double click the OLE DB Destination.
- In the OLE DB Destination Editor Window, press New... for the OLE DB connection manager.
- In the Configure OLE DB Connection Manager, press the New... button.
- In the Connection Manager, in the Provider combo box, select Native OLE DB\SQL Server Native Client.
- 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).
- Select the Log on to the server information (in this example Windows Authentication is used).
- 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.
- 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).
- In the OLE DB Destination Editor, press the Mappings page and then press OK.
- 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.
- To verify that the data was imported successfully, open SQL Server Management Studio.
- Go to the database (in this example, Adventureworks) and open the myImages table.
- 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:
About the author
View all my tips
Article Last Updated: 2012-04-11