我是SSIS,Visual Studio的新手。想一想在开始时可能会提到。
我想要实现的是从http://www.ads-slo.org/statistika/网站下载某个xls文件并将其存储在我的计算机上的某个文件夹中。我已经实现了,但问题是我知道如何一次一个文件。我通过打开新连接,进入http连接并在管理器中键入服务器URL来实现它:在我的情况下,如果我们说从2016年1月开始就是这样:http://www.ads-slo.org/media/xls/2016/Januar-2016.xls。在这样做之后,我构建了一个脚本任务,或者或多或少地从一个网站上复制它,根据连接管理器下载给定特定URL的文件。
我的问题是我想下载此网站上的所有文件,因此从2007年1月开始到2016年1月结束,使用单个软件包并且不更改我的连接管理器服务器网址设置100次。
有什么方法可以帮助我。我将永远感激。
提前谢谢。
亲切的问候,Domen
答案 0 :(得分:0)
这是一个非常简单的示例(可以通过使用脚本任务动态更改连接字符串,可以改进 - 请参阅代码块之后的注释)。您还可以使用表达式和Connection Manager的表达式属性动态更改连接字符串。但是,由于您使用脚本任务来处理下载,我已使用一个来演示它。
由于您没有标记您正在使用的脚本语言(VB或C#),我已经在VB中编写了草稿。
我添加了注释,但stackoverflow语法突出显示奇怪地解释它;道歉。
Public Sub Main()
' Get the HTTP Connection
Dim connObj As Object = Dts.Connections("HTTP Connection Manager").AcquireConnection(Nothing)
Dim connection As New HttpClientConnection(connObj)
' Static list of month names for dynamic connection string (obviously add as many as needed)
Dim monthNames As String() = New String() {"Januar", "February", "March"}
' Nested loop - for each year and month, try to download the Excel file
For Y As Integer = 2007 To 2016 Step 1
For M As Integer = 0 To monthNames.Length - 1 Step 1
' Set the assumed name of the remote file
Dim remoteFileName As String = monthNames(M) + "-" + Y.ToString() + ".xls"
' Change the connection string a.k.a dynamic connection string
connection.ServerURL = "http://www.ads-slo.org/media/xls/" + Y.ToString() + "/" + remoteFileName
' Set where to download the file to
Dim localFileName As String = "C:\Temp\" + remoteFileName
Try
connection.DownloadFile(localFileName, True)
Dim buffer As Byte() = connection.DownloadData()
Dim data As String = Encoding.ASCII.GetString(buffer)
Catch E As Exception
' File may not exist on remote server, delete the blank copy it attempted to create
File.Delete(localFileName)
End Try
Next
Next
Dts.TaskResult = DTSExecResult.Success
End Sub
如何改进?
一个潜在的改进是使用HttpWebRequest解析远程服务器中的文件夹和目录内容(以保存包含月份名称的静态列表,硬编码的开始和结束年份以及构建文件名)。
但是,远程服务器权限可能存在允许进行此类请求的问题,因此您必须进一步调查并与服务器管理员联系。
测试上述代码后,它成功从网站上下载了Januar-2015和Januar-2016 Excel文件。