检查 HTTP 位置中是否有空的 csv 文件



我正在尝试按照确定文件是否为空 (SSIS( 以查看该文件在 HTTP 位置是否为空。我无法首先下载它,因为该过程卡在源头并且不会让我的包完成。我想做的只是查询源文件,如果为0条记录,则退出进程并发送电子邮件。

我有电子邮件部分工作,但我不确定如何在源头检查 0 条记录。我使用 vb.net 作为我的脚本语言。以下是我到目前为止的片段:

Public Sub Main()
        ' Get the unmanaged connection object, from the connection manager called "HTTP Connection Manager"
        Dim nativeObject As Object = Dts.Connections("HTTP_FileName").AcquireConnection(Nothing)
        ' Create a new HTTP client connection
        Dim connection As New HttpClientConnection(nativeObject)

        ' Download the file #1
        ' Save the file from the connection manager to the local path specified
        Dim filename As String = "DestinationPath"
        connection.DownloadFile(filename, True)
        Dts.TaskResult = ScriptResults.Success
    End Sub

编辑

如果不是 0 条记录,即使在 HTTP 位置检查 0kb 文件也应该达到目的。从技术上讲,它可以检查文件大小,然后使脚本失败,从而引发相应的失败消息。

如果您使用的是 .Net Framework 4 Microsoft您可以使用 System.Net 库来实现这一点:

您可以使用以下代码:

Imports System.Net
Public Sub Main()
    ' Get the unmanaged connection object, from the connection manager called "HTTP Connection Manager"
    Dim nativeObject As Object = Dts.Connections("HTTP_FileName").AcquireConnection(Nothing)
    ' Create a new HTTP client connection
    Dim connection As New HttpClientConnection(nativeObject)
    Dim webStream As IO.Stream
    Dim req As HttpWebRequest
    Dim res As HttpWebResponse
'Assuming that Dts.Connections("HTTP_FileName").ConnectionString Contains the file URL
    req = WebRequest.Create(Dts.Connections("HTTP_FileName").ConnectionString)
    req.Method = "GET" ' Method of sending HTTP Request(GET/POST)
    res = req.GetResponse() ' Send Request
    webStream = res.GetResponseStream() ' Get Response
    Dim webStreamReader As New IO.StreamReader(webStream)
    If webStreamReader.ReadToEnd.Length = 0 Then
          msgbox("File is Empty")
    Else
          Dim filename As String = "DestinationPath"
          connection.DownloadFile(filename, True)              
    End If
    Dts.TaskResult = ScriptResults.Success
End Sub

注意:@NoAlias提供的方式也有效

If connection.DownloadData().Length = 0 Then
 'Logic for when there is no data.
End If

基于问题的编辑:

如果只是检查响应的长度,则可以执行以下操作:

If connection.DownloadData().Length = 0 Then
     'Logic for when there is no data.
End If

您可以获取原始字符串数据并通过以下方式验证文件:

Dim strFileContents = System.Text.Encoding.UTF8.GetString(connection.DownloadData())
'Example file has header row but no content rows.  Your specific validation will be based 
'on what you're expecting from an empty csv file. 
If strFileContents Is Nothing OrElse  strFileContents.Split(System.Environment.NewLine).Count < 2 Then
     'Empty file.
End If

最新更新