Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Downloading multiple files from internet with SSIS and C#

$
0
0
Introduction

Sometimes we need to download the files from the FTP, a Web Site or another resource as the source data for an ETL process.In this article, we will show how to download multiple files from internet using SQL Server Integration Services (SSIS) and the script task. We will learn 4 things:

We will first show how to download a single file using the Script task. Secondly, we will show how to download multiple files using the foreach loop>item enumerator and the script task. We will also show how to send all the files to download using a C# array. Finally, we will use the ADO object enumerator using the foreach loop with the script task. Requirements SQL Server installed. SSIS and SQL Server Data Tools (SSDT). Excel installed (for the example 3 only) How to Download a Single File Using the SSIS Script Task

In the first example, we will download a single file from internet. You can use any website of your preference. In this example, we are using a website named www.textfiles.com/adventure . This URL contains several files to download:


Downloading multiple files from internet with SSIS and C#

We will open SSDT and create a new SSIS project. In the SSIS Project, go to the menu and click SSIS>Variables:


Downloading multiple files from internet with SSIS and C#

We will create a string variable named filename and specify the name of the internet text to download. In this example, 221baker.txt is the name of one of the files on the www.textfiles.com\adventure web site:


Downloading multiple files from internet with SSIS and C#

In SSIS Toolbox in the SSIS project, drag and drop the Script Task:


Downloading multiple files from internet with SSIS and C#

You can program in C# (by default) or VB. We will work in C# this time. Also in ReadWriteVariables, add the filename variable created before and press Edit Script:


Downloading multiple files from internet with SSIS and C#

In the C# code expand region and add the System.Net namespace. System.Net will be used to download files in internet:


Downloading multiple files from internet with SSIS and C#

In the public void main function, add this code:

try
{
string myURI = "http://www.textfiles.com/adventure/";
string fileName = Dts.Variables["User::filename"].Value.ToString(), myWebString = null, myLocalPath = null;
myWebString = myURI + fileName;
myLocalPath = "c:\\sql\\" + fileName;
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(myWebString, myLocalPath);
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.Events.FireError(18, "The process failed", ex.ToString(), "", 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}

The code will download a file named 221baker.txt. We first use the try and catch code.Try and catch are used to handle errors. If the code in the try fails, catch will show the error message.

We have in the code the variable definition. myURI stores the website URL with the files. The filename is a string that will receive the SSIS variable named filename created previously. The SSIS variable contains the filename. myWebString is the web concatenated with the filename. myLocalPath is the local drive where we will store the file created (c:\sql).

string myURI = "http://www.textfiles.com/adventure/";
string fileName = Dts.Variables["User::filename"].Value.ToString(), myWebString = null, myLocalPath = null;
myWebString = myURI + fileName;
myLocalPath = "c:\\sql\\" + fileName;

The next section is used to download the file. We create a webclient object and then we use the download function to download the file using the variables created before:

WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(myWebString, myLocalPath);

Once the code is done, save it and close it. Right click the script task and select the Execute Task option:


Downloading multiple files from internet with SSIS and C#

If everything is OK, you will be able to find the 221baker.txt file in your local c:\sql folder:


Downloading multiple files from internet with SSIS and C#
How to Download Multiple files Using the Foreach Loop

In the first example, we learned how to download a single file. In this new example, we will download several files from a list using the Foreach Loop Container. This Container is used to use a Loop a task. The Item enumerator allows creating a list manually. In this example, we will create a list of files and the script task will copy the files using that list.

In SSIS, double click the Foreach Loop Container:


Downloading multiple files from internet with SSIS and C#

In the design pane, move the script task created in the example one inside the Container:


Downloading multiple files from internet with SSIS and C#

In the Foreach Loop Container, select Foreach Item Enumerator and press the Column button:


Downloading multiple files from internet with SSIS and C#
In the column created, add the list of files names in the web:
Downloading multiple files from internet with SSIS and C#

In Variable Mapping, select the filename variable:


Downloading multiple files from internet with SSIS and C#

If everything is OK, you will have all the files of the list created when you run the package.

How to Send All the Files to Download Using a C# Array

The example 2 is the best option if you have few files. However, in our web page, there more than 400 files in the website. It would take a long time to copy and paste the names using the item enumerator. The new example will show how to create an array with the list of files.

We will first go to www.textfiles.com/adventure and copy the filenames:


Downloading multiple files from internet with SSIS and C#

Copy and paste the results into Excel:


Downloading multiple files from internet with SSIS and C#

We will pivot the table to create a list of files in the format requested by C#. Select the Filename column and go to INSERT>Pivot Table:


Downloading multiple files from internet with SSIS and C#

In Create PivotTable, press OK:


Downloading multiple files from internet with SSIS and C#
Drag Filename to COLUMNS:
Downloading multiple files from internet with SSIS and C#

Copy the file names to a new Excel file:


Downloading multiple files from internet with SSIS and C#

Save the file as a CSV file:


Downloading multiple files from internet with SSIS and C#

You will receive a message about some features that will be lost. Press Yes:


Downloading multiple files from internet with SSIS and C#
Open the csv file with a text editor:

Viewing all articles
Browse latest Browse all 3160

Trending Articles