Exporting Images With SSIS: Previous Article Export Column Transformation
Exporting Images With SSIS: Previous Article Export Column Transformation
Exporting Images With SSIS: Previous Article Export Column Transformation
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\
2) OLE DB Source Add an OLE DB Source component that reads from the table ImageStore. We need the columns imageName and imageFile.
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.
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
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
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.
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)
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.
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
This example only replaces the Flat File Source Component from example A into a Script Component that reads all files in a certain folder.
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\
B2) Script Component Add a Script Component to the Data Flow and select Source as the Script Component Type.
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).
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
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
10
<CLSCompliant(False)> _
26
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.
B7) The result Run the package and watch the table for the result.
The result
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\
C2) Script Component Add a Script Component to the Data Flow and select Source as the Script Component Type.
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]).
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
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
// Loop through the files in the folder foreach (string currentFile in allFiles) {
21 22 23 24 25 26
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
' Added
Inherits UserComponent
1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2
' 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)
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
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.
C7) The result Run the package and watch the table for the result.
The result