export images
TRANSCRIPT
-
8/2/2019 Export Images
1/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 ColumnTransformation .
Solution This example assumes that you have a filled table named ImageStore which I created in theprevious article.12345
CREATE TABLE [dbo].[ImageStore]( [id] [int] IDENTITY(1,1) NOT NULL, [imageName] [nvarchar](50) NULL, [imageFile] [varbinary](max) NULL
) 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
http://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.htmlhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.htmlhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.htmlhttp://msdn.microsoft.com/en-us/library/ms139818.aspxhttp://msdn.microsoft.com/en-us/library/ms139818.aspxhttp://msdn.microsoft.com/en-us/library/ms139818.aspxhttp://msdn.microsoft.com/en-us/library/ms139818.aspxhttp://1.bp.blogspot.com/-lgxKSvi3eUw/TjQAlDryE2I/AAAAAAAAAX8/mJ4SUIq3Pb8/s1600/importimages16.jpghttp://1.bp.blogspot.com/-uiXqk8KRHaY/TjP8McnhGXI/AAAAAAAAAX4/IBNB99P0SN8/s1600/importimages15.jpghttp://1.bp.blogspot.com/-lgxKSvi3eUw/TjQAlDryE2I/AAAAAAAAAX8/mJ4SUIq3Pb8/s1600/importimages16.jpghttp://1.bp.blogspot.com/-uiXqk8KRHaY/TjP8McnhGXI/AAAAAAAAAX4/IBNB99P0SN8/s1600/importimages15.jpghttp://msdn.microsoft.com/en-us/library/ms139818.aspxhttp://msdn.microsoft.com/en-us/library/ms139818.aspxhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html -
8/2/2019 Export Images
2/30
3) Derived Column With the folderpath from the variable and the filename from the column imageName wecreate an export filepath which is needed for the Export Column component. Name itNewFilePath.
Expression: @[User::ExportFolder] + imageName
http://3.bp.blogspot.com/-HxUcvMkXiLw/TjQBU9fOhMI/AAAAAAAAAYA/IcgUl9cFxnQ/s1600/importimages17.jpg -
8/2/2019 Export Images
3/30
4) Export Column Add an Export Column component and select the imageFile column as Extract Column andthe NewFilePath column as File Path Column .
Export Column
5) The result Run the package and watch the folder for the result.
http://2.bp.blogspot.com/-DpQz6ybk7yE/TjQCaFXqHGI/AAAAAAAAAYE/6qhXBocO3JM/s1600/importimages18.jpg -
8/2/2019 Export Images
4/30
The result
Note: it also works with other filetypes, not just images.
http://2.bp.blogspot.com/-lmO12NK9PNg/TjRWeTqs5pI/AAAAAAAAAYI/Exb5EvfItp4/s1600/importimages19.jpg -
8/2/2019 Export Images
5/30
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. Youthrow in a filepath and it creates a blob column with binary data. But the Script Component can beuseful 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.jpgC:\Users\Joost\Pictures\MountainBike2.jpg
C:\Users\Joost\Pictures\MountainBike3.jpg
My target table looks like this:
1
2
3
4
5
CREATE TABLE [dbo].[ImageStore](
[id] [int] IDENTITY(1,1) NOT NULL,
[imageName] [nvarchar](50) NULL,
[imageFile] [varbinary](max) NULL
) ON [PRIMARY]
Note: If you want to store images in a SQL Server table use the VARBINARY datatype because theIMAGE 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.
http://msdn.microsoft.com/en-us/library/ms141262.aspxhttp://msdn.microsoft.com/en-us/library/ms141262.aspxhttp://msdn.microsoft.com/en-us/library/ms141262.aspxhttp://microsoft-ssis.blogspot.com/2011/08/exporting-images-with-ssis.htmlhttp://microsoft-ssis.blogspot.com/2011/08/exporting-images-with-ssis.htmlhttp://microsoft-ssis.blogspot.com/2011/08/exporting-images-with-ssis.htmlhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Ahttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Ahttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Bhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Bhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Chttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Chttp://www.blogger.com/http://www.blogger.com/http://www.blogger.com/http://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Chttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Bhttp://microsoft-ssis.blogspot.com/2011/08/importing-images-with-ssis.html#Ahttp://microsoft-ssis.blogspot.com/2011/08/exporting-images-with-ssis.htmlhttp://msdn.microsoft.com/en-us/library/ms141262.aspx -
8/2/2019 Export Images
6/30
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 getthe filename from the filepath. Add a Derived Column and add a new column named FileName. Theexpression is:RIGHT(ImagePath,FINDSTRING(REVERSE(ImagePath),"\\",1) - 1)
http://4.bp.blogspot.com/-QfUl8guYYto/TjOnAlofHXI/AAAAAAAAAW8/y5Fi0Yhddcc/s1600/importimages1.jpghttp://microsoft-ssis.blogspot.com/2011/06/often-used-ssis-expressions.htmlhttp://microsoft-ssis.blogspot.com/2011/06/often-used-ssis-expressions.htmlhttp://microsoft-ssis.blogspot.com/2011/06/often-used-ssis-expressions.htmlhttp://4.bp.blogspot.com/-QfUl8guYYto/TjOnAlofHXI/AAAAAAAAAW8/y5Fi0Yhddcc/s1600/importimages1.jpghttp://microsoft-ssis.blogspot.com/2011/06/often-used-ssis-expressions.html -
8/2/2019 Export Images
7/30
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.
http://1.bp.blogspot.com/-Jay4JbonCbs/TjOpmAsKo_I/AAAAAAAAAXA/SeV1D9sqmOI/s1600/importimages2.jpg -
8/2/2019 Export Images
8/30
Import Column
A4) Input Columns Edit the Import Column transformation and go to the Input Columns tab. Select the column thatcontains the filepath of the images.
http://3.bp.blogspot.com/-IRLGMNiWhVw/TjOq-GP4XtI/AAAAAAAAAXE/Fb4CNAeDgYI/s1600/importimages3.jpghttp://3.bp.blogspot.com/-IRLGMNiWhVw/TjOq-GP4XtI/AAAAAAAAAXE/Fb4CNAeDgYI/s1600/importimages3.jpg -
8/2/2019 Export Images
9/30
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 andgive it the name ImageData. Remember the generated ID because you need it for the next step.
http://4.bp.blogspot.com/-Lsv5kzOT0F4/TjOs45SnOCI/AAAAAAAAAXI/EPZbjbkPJuQ/s1600/importimages4.jpghttp://4.bp.blogspot.com/-Lsv5kzOT0F4/TjOs45SnOCI/AAAAAAAAAXI/EPZbjbkPJuQ/s1600/importimages4.jpg -
8/2/2019 Export Images
10/30
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).
http://4.bp.blogspot.com/-cO6tZJwO7Xk/TjO0K02Q8mI/AAAAAAAAAXQ/tut8QgMCZVY/s1600/importimages5.jpghttp://4.bp.blogspot.com/-cO6tZJwO7Xk/TjO0K02Q8mI/AAAAAAAAAXQ/tut8QgMCZVY/s1600/importimages5.jpg -
8/2/2019 Export Images
11/30
FileDataColumnId
A7) OLE DB Destination Now you're ready to import the data in the database table. Add an OLE DB Destination and selectthe table ImageStore. Connect FileName to imageName and ImageData to imageFile.
http://1.bp.blogspot.com/-r22WEIbqkVE/TjO0w7NLlXI/AAAAAAAAAXU/1WjE-l-SSHI/s1600/importimages6.jpghttp://1.bp.blogspot.com/-r22WEIbqkVE/TjO0w7NLlXI/AAAAAAAAAXU/1WjE-l-SSHI/s1600/importimages6.jpg -
8/2/2019 Export Images
12/30
OLE DB Destination
A8) The result Run the package and watch the table for the result.
http://1.bp.blogspot.com/-3CU3CDyMZjU/TjPV6aLHMDI/AAAAAAAAAXY/kBNWm2D_5nU/s1600/importimages7.jpghttp://1.bp.blogspot.com/-3CU3CDyMZjU/TjPV6aLHMDI/AAAAAAAAAXY/kBNWm2D_5nU/s1600/importimages7.jpg -
8/2/2019 Export Images
13/30
The result
B) Importing images with Script Component and Import
Column
http://2.bp.blogspot.com/-k669uLeXzi8/TjPbKqON3WI/AAAAAAAAAXc/FA4CZY4t3Ho/s1600/importimages8.jpghttp://www.blogger.com/http://www.blogger.com/http://2.bp.blogspot.com/-k669uLeXzi8/TjPbKqON3WI/AAAAAAAAAXc/FA4CZY4t3Ho/s1600/importimages8.jpghttp://www.blogger.com/ -
8/2/2019 Export Images
14/30
This example only replaces the Flat File Source Component from example A into a Script Componentthat 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\
http://4.bp.blogspot.com/-wWHQciIXlAY/TjU9ic6wSBI/AAAAAAAAAYM/mnxNxPt3mJU/s1600/importimages20.jpg -
8/2/2019 Export Images
15/30
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
http://3.bp.blogspot.com/-xfsUBqhsLp0/TjP7v34dIWI/AAAAAAAAAX0/zIWAzJb1G3M/s1600/importimages14.jpghttp://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://3.bp.blogspot.com/-xfsUBqhsLp0/TjP7v34dIWI/AAAAAAAAAX0/zIWAzJb1G3M/s1600/importimages14.jpghttp://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://3.bp.blogspot.com/-xfsUBqhsLp0/TjP7v34dIWI/AAAAAAAAAX0/zIWAzJb1G3M/s1600/importimages14.jpg -
8/2/2019 Export Images
16/30
B3) Select variable Select the variable from step 1 as a ReadOnly variable.
Select ReadOnlyVariables
http://2.bp.blogspot.com/-nQBdHgKhGlU/TjPuGU6npoI/AAAAAAAAAXo/9HxopqkA1nM/s1600/importimages11.jpg -
8/2/2019 Export Images
17/30
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
http://2.bp.blogspot.com/-20Q6cLoqXTk/TjVBORDno1I/AAAAAAAAAYQ/GIT0EHh8oME/s1600/importimages21.jpg -
8/2/2019 Export Images
18/30
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
9
10
11
12
1314
15
16
17
18
19
20
21
22
23
24
//C# Code
using System;
using System.Data;
using System.IO; // Added
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
// Get all files within the folder
string[] allFiles =Directory.GetFiles(this.Variables.imageLocation.ToString());
// 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
-
8/2/2019 Export Images
19/30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
' VB.Net Code
Imports System
Imports System.Data
Imports System.IO ' Added
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
_
_
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub CreateNewOutputRows()
' Get all files within the folder
Dim allFiles() As String =Directory.GetFiles(Me.Variables.imageLocation.ToString())
' 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
Next
End Sub
End Class
B6) Restore Invalid Column References
-
8/2/2019 Export Images
20/30
Connect the new Script Component to the Derived Column and map the old imagePath column tonew imagePath Column by double clicking on the Derived Column and pressing OK to confirm themapping.
Restore Invalid Column References editor
B7) The result Run the package and watch the table for the result.
http://2.bp.blogspot.com/-jIQKOo-1oAI/TjVF3Mo2oII/AAAAAAAAAYU/5M6-FqZGpfY/s1600/importimages22.jpg -
8/2/2019 Export Images
21/30
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
http://1.bp.blogspot.com/-9ArZcd0gfYE/TjVHnGA-LUI/AAAAAAAAAYY/ftmBfHoEQZs/s1600/importimages23.jpghttp://www.blogger.com/http://1.bp.blogspot.com/-9ArZcd0gfYE/TjVHnGA-LUI/AAAAAAAAAYY/ftmBfHoEQZs/s1600/importimages23.jpghttp://www.blogger.com/ -
8/2/2019 Export Images
22/30
-
8/2/2019 Export Images
23/30
Script Component Source
C3) Select variable
Select the variable from step 1 as a ReadOnly variable.
Select ReadOnlyVariables
http://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://4.bp.blogspot.com/-6alTdNeb04g/TjPrd80DLvI/AAAAAAAAAXg/h7PzztlmrGc/s1600/importimages9.jpghttp://2.bp.blogspot.com/-nQBdHgKhGlU/TjPuGU6npoI/AAAAAAAAAXo/9HxopqkA1nM/s1600/importimages11.jpg -
8/2/2019 Export Images
24/30
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
http://4.bp.blogspot.com/-0y9DI1vo9xE/TjPsoh7mHkI/AAAAAAAAAXk/YiPptTsxnKk/s1600/importimages10.jpg -
8/2/2019 Export Images
25/30
C5) The Script Edit the Script and replace the script with this one (we only need CreateNewOutputRows).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// C# Code
using System;
using System.Data;
using System.IO; // Added
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
// Get all files within the folder
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)
{
-
8/2/2019 Export Images
26/30
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Fill fileInfo variable with file information
fileInfo = new FileInfo(currentFile);
// Create new record
this.Output0Buffer.AddRow();
// Fill filename column
Output0Buffer.imageName = fileInfo.Name;
// Stream the file into a byte[] object which can be used ascolumn data
FileStream fs = File.OpenRead(fileInfo.FullName);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
// Fill column and close filestream
Output0Buffer.imageFile.AddBlobData(b);
fs.Close();
}
}
}
or VB.Net
1
2
3
4
' VB.Net code
Imports System
Imports System.Data
Imports System.IO ' Added
Imports System.Math
-
8/2/2019 Export Images
27/30
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
920
21
22
2
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
_
_
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub CreateNewOutputRows()
' Get all files within the folder
Dim allFiles As String() =Directory.GetFiles(Me.Variables.imageLocation.ToString())
' 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
-
8/2/2019 Export Images
28/30
3
24
25
26
27
28
29
30
31
3
2
33
34
35
36
37
38
39
4
' Stream the file into a byte() object which can be used ascolumn data
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()
Next
End Sub
End Class
-
8/2/2019 Export Images
29/30
0
41
42
43
44
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 selectthe table ImageStore. Connect imageName to imageName and imageFile to imageFile.
Connect input and destination columns
http://microsoft-ssis.blogspot.com/2011/02/script-task-and-component-logging.htmlhttp://microsoft-ssis.blogspot.com/2011/02/script-task-and-component-logging.htmlhttp://microsoft-ssis.blogspot.com/2011/02/script-task-and-component-logging.htmlhttp://1.bp.blogspot.com/-HGMSrj7gwvQ/TjP1bodg6KI/AAAAAAAAAXs/pe8TvHJw_ic/s1600/importimages12.jpghttp://microsoft-ssis.blogspot.com/2011/02/script-task-and-component-logging.html -
8/2/2019 Export Images
30/30
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
http://3.bp.blogspot.com/-7B67j2qEb6k/TjP2xNr8paI/AAAAAAAAAXw/Sx8nfJ4UeCg/s1600/importimages13.jpg