Vb.Net - 遍历数据库中的文件名,然后下载它们

时间:2012-11-26 08:44:50

标签: vb.net

需要帮助,因为努力编写代码:

下面的函数将遍历数据库以获取文件名,然后下载这些文件(如果存在)。我在努力编写代码“在这里下载文件代码”。还需要它返回ARRAY,它应该是已在此代码中下载的文件的名称。

任何人都可以帮忙。

Public Function DownloadFile(ByVal SourcePath As String, ByVal TargetPath As String) As Boolean


    Dim DownloadFile As Boolean = False
    Dim lvar_FileName As String = Nothing
    Dim lvar_FileType As String = Nothing

    Using Conn As New SqlConnection(ConnString)
        SQLCommand = New SqlCommand("File_List",Conn)
        SQLCommand.CommandText = "Select File_Name from File_List "
        Try
            Conn Conn.Open()
            Dim reader As SqlDataReader
            reader = SQLCommand.ExecuteReader()
            While reader.Read()
                'Code Here
                lvar_FileName = reader(0)

        'Download Files Code here

            End While
        Catch ex As Exception

        End Try
    End Using

    Return DownloadFile 

End Function

此致

1 个答案:

答案 0 :(得分:0)

我可以返回List(Of String)而不是数组,因为它可以调整大小:

Public Function DownloadFile(ByVal SourcePath As String, ByVal lvar_TargetPath As String) As List(Of String)
    Dim allFiles As New List(Of String) 
    Using Conn As New SqlConnection(ConnString)
        Using SQLCommand = New SqlCommand("Select File_Name from File_List", Conn)
            Try
                Conn.Open()
                Using reader = SQLCommand.ExecuteReader()
                    While reader.Read()
                        Dim fileName = reader.GetString(0)
                        Dim destPath = Path.Combine(SourcePath, fileName)
                        allFiles.Add(destPath)
                    End While
                End Using
            Catch ex As Exception
                ' Log exception here, otherwise don't catch it
            End Try
        End Using
    End Using
    Return allFiles
End Function