Sie sind auf Seite 1von 30

Exporting images with SSIS

Case
In a previous article I showed you how to import images (or other files) with SSIS into a SQL Server table. This article shows you how to get them out again with the Export Column Transformation.

Solution
This example assumes that you have a filled table named ImageStore which I created in the previous article. 1CREATE TABLE [dbo].[ImageStore]( 2 [id] [int] IDENTITY(1,1) NOT NULL, 3 [imageName] [nvarchar](50) NULL, 4 [imageFile] [varbinary](max) NULL 5) ON [PRIMARY] 1) Variables Add a string variable to the package to store the path of the export folder. I called mine ExportFolder. Fill it with a value like: C:\Users\Joost\Pictures\Export\

Variable to store the export folder

2) OLE DB Source Add an OLE DB Source component that reads from the table ImageStore. We need the columns imageName and imageFile.

OLE DB Source component

3) Derived Column With the folderpath from the variable and the filename from the column imageName we create an export filepath which is needed for the Export Column component. Name it NewFilePath.

Expression: @[User::ExportFolder] + imageName

4) Export Column Add an Export Column component and select the imageFile column as Extract Column and the NewFilePath column as File Path Column.

Export Column

5) The result Run the package and watch the folder for the result.

The result

Note: it also works with other filetypes, not just images.

Importing images with SSIS

Case
I want to import files (images) with SSIS to a SQL Server table. And export those images to an other folder.

Solution
The easiest way to import files/images with SSIS, is with the Import Column Transformation. You throw in a filepath and it creates a blob column with binary data. But the Script Component can be useful too. For export you can use the Export Column Transformation. Three examples: A) Importing images with Import Column B) Importing images with Script Component and Import Column C) Importing images with Script Component

A) Importing images with Import Column


For the first example I have created a CSV file with filepaths of images named bikes.csv: C:\Users\Joost\Pictures\MountainBike1.jpg C:\Users\Joost\Pictures\MountainBike2.jpg C:\Users\Joost\Pictures\MountainBike3.jpg

My target table looks like this: 1CREATE TABLE [dbo].[ImageStore]( 2 [id] [int] IDENTITY(1,1) NOT NULL, 3 [imageName] [nvarchar](50) NULL, 4 [imageFile] [varbinary](max) NULL 5) ON [PRIMARY]

Note: If you want to store images in a SQL Server table use the VARBINARY datatype because the IMAGE datatype is due to be removed from SQL Server. A1) Flat File Source Go to your Data Flow and use a Flat File Source to read the CSV file from above.

Import file with only one column: ImagePath

A2) Get filename from path I also want to store the original filename in my table. I use an expression in a Derived Column to get the filename from the filepath. Add a Derived Column and add a new column named FileName. The expression is: RIGHT(ImagePath,FINDSTRING(REVERSE(ImagePath),"\\",1) - 1)

Extract filename from filepath

A3) Import Column Now the actual importing of the image files into SSIS. Add a Import Column to the Control Flow.

Import Column

A4) Input Columns Edit the Import Column transformation and go to the Input Columns tab. Select the column that contains the filepath of the images.

Select column

A5) Input and Output Properties Go to the Input and Output Properties tab and add a new output column of the type DT_IMAGE and give it the name ImageData. Remember the generated ID because you need it for the next step.

New output column: DT_IMAGE

A6) Connect input and output Now go to the Input Columns. Select the input column and change the FileDataColumnId to the ID of the output column (24 in my case).

FileDataColumnId

A7) OLE DB Destination Now you're ready to import the data in the database table. Add an OLE DB Destination and select the table ImageStore. Connect FileName to imageName and ImageData to imageFile.

OLE DB Destination

A8) The result Run the package and watch the table for the result.

The result

B) Importing images with Script Component and Import Column

This example only replaces the Flat File Source Component from example A into a Script Component that reads all files in a certain folder.

Replace CSV file by Script Component

B1) Variable Add a string variable to the package to store the path of the folder with images. I called mine imageLocation. Fill it with a value like: C:\Users\Joost\Pictures\

Variable to store the import folder

B2) Script Component Add a Script Component to the Data Flow and select Source as the Script Component Type.

Script Component Source

B3) Select variable Select the variable from step 1 as a ReadOnly variable.

Select ReadOnlyVariables

B4) Inputs and Outputs Go to the Inputs and Outputs tab and add an output column: imageName (Unicode string [DT_WSTR] 150).

Column for storing the image path

B5) The Script Edit the Script and replace the script with this one (we only need CreateNewOutputRows). 1 2 3 4 5 6 7 8 [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 9 public class ScriptMain : UserComponent 10{ 11 12 13
public override void CreateNewOutputRows() { // Get all files within the folder string[] allFiles = //C# Code using System; using System.Data; using System.IO; // Added

using Microsoft.SqlServer.Dts.Pipeline.Wrapper; using Microsoft.SqlServer.Dts.Runtime.Wrapper;

14Directory.GetFiles(this.Variables.imageLocation.ToString()); 15 16 17 18 19 20 21 22 23 24
} } } // Loop through the files in the folder foreach (string currentFile in allFiles) { // Create a new record foreach file this.Output0Buffer.AddRow(); Output0Buffer.ImagePath = currentFile;

Or VB.Net

1
' VB.Net Code

2 3 4

Imports System Imports System.Data Imports System.IO ' Added

5 Imports System.Math 6 Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper 7 Imports Microsoft.SqlServer.Dts.Runtime.Wrapper 8 9 <Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute>


_

10

<CLSCompliant(False)> _

11Public Class ScriptMain 12 13 14 15 16 17 18 19 20 21 22 23 24 25


End Class Next End Sub ' Loop through the files in the folder Dim currentFile As String For Each currentFile In allFiles ' Create a new record foreach file Me.Output0Buffer.AddRow() Output0Buffer.imagePath = currentFile Public Overrides Sub CreateNewOutputRows() ' Get all files within the folder Inherits UserComponent

Dim allFiles() As String = Directory.GetFiles(Me.Variables.imageLocation.ToString())

26

B6) Restore Invalid Column References

Connect the new Script Component to the Derived Column and map the old imagePath column to new imagePath Column by double clicking on the Derived Column and pressing OK to confirm the mapping.

Restore Invalid Column References editor

B7) The result Run the package and watch the table for the result.

The result

C) Importing images with Script Component


This example uses a Script Component to import all files in a folder to the same database table as in

the first examples. The Script Component replaces all components except the OLE DB Destination. The Script Component loads the file into a Bit array via a FileStream object. That array can be used to fill an image column in SSIS. Note: this is just an example to show that there are other ways to import files. Where possible use the Import Column Transformation. C1) Variable Add a string variable to the package to store the path of the folder with images. I called mine imageLocation. Fill it with a value like: C:\Users\Joost\Pictures\

Variable to store the import folder

C2) Script Component Add a Script Component to the Data Flow and select Source as the Script Component Type.

Script Component Source

C3) Select variable Select the variable from step 1 as a ReadOnly variable.

Select ReadOnlyVariables

C4) Inputs and Outputs Go to the Inputs and Outputs tab and add two output columns: imageName (Unicode string [DT_WSTR] 50) and imageFile (image [DT_IMAGE]).

Create two output columns

C5) The Script Edit the Script and replace the script with this one (we only need CreateNewOutputRows). 1 // C# Code 2 using System; 3 using System.Data; 4 5 6 7
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] using System.IO; // Added

using Microsoft.SqlServer.Dts.Pipeline.Wrapper; using Microsoft.SqlServer.Dts.Runtime.Wrapper;

8 9

public class ScriptMain : UserComponent { public override void CreateNewOutputRows() { // Get all files within the folder

10 11 12 13 14 15 16 17 18 19 20

string[] allFiles = Directory.GetFiles(this.Variables.imageLocation.ToString());

// Variable for storing file properties FileInfo fileInfo;

// Loop through the files in the folder foreach (string currentFile in allFiles) {

21 22 23 24 25 26

// Fill fileInfo variable with file information fileInfo = new FileInfo(currentFile);

// Create new record this.Output0Buffer.AddRow();

// Fill filename column

27
Output0Buffer.imageName = fileInfo.Name;

28 29 30column data 31 32 33 34
// Fill column and close filestream FileStream fs = File.OpenRead(fileInfo.FullName); byte[] b = new byte[fs.Length]; fs.Read(b, 0, b.Length); // Stream the file into a byte[] object which can be used as

35 36 37 38 39} 40 41
} }

Output0Buffer.imageFile.AddBlobData(b); fs.Close();

or VB.Net 1 2
Imports System.Data ' VB.Net code Imports System

3 4

Imports System.IO Imports System.Math

' Added

5 Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper 6 Imports Microsoft.SqlServer.Dts.Runtime.Wrapper 7 8


<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute( )> _

9 <CLSCompliant(False)> _ 1 Public Class ScriptMain 0 1 1 1 2


Public Overrides Sub CreateNewOutputRows() ' Get all files within the folder

Inherits UserComponent

Dim allFiles As String() = 1 Directory.GetFiles(Me.Variables.imageLocation.ToString()) 3

1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2

' Variable for storing file properties Dim fileInfo As FileInfo

' Loop through the files in the folder Dim currentFile As String For Each currentFile In allFiles

' Fill fileInfo variable with file information fileInfo = New FileInfo(currentFile)

' Create new record Me.Output0Buffer.AddRow()

' Fill filename column Output0Buffer.imageName = fileInfo.Name

3
column data

' Stream the file into a byte() object which can be used as

2 4 2 5 2 6 2 7 2 8
Next End Sub

Dim fs As FileStream = File.OpenRead(fileInfo.FullName) Dim b(fs.Length - 1) As Byte fs.Read(b, 0, b.Length)

' Fill column and close filestream Output0Buffer.imageFile.AddBlobData([b]) fs.Close()

2 9 End Class 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4

0 4 1 4 2 4 3 4 4

Note: Don't forget to add error handling and logging. C6) OLE DB Destination Now you're ready to import the data in the database table. Add an OLE DB Destination and select the table ImageStore. Connect imageName to imageName and imageFile to imageFile.

Connect input and destination columns

C7) The result Run the package and watch the table for the result.

The result

Note: it also works with other filetypes, not just images