我正在尝试从ftp下载.csv文件,然后将数据从下载的文件导入到sql表中。但是它给了我错误"访问被拒绝了路径"但是当我没有代码访问它时我可以看到文件和文件也没有从ftp下载。
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form1
Sub Get_File_From_FTP()
Try
Dim RFN = "ftp://someaddress.com/Test.csv"
Dim LFN = "C:\Sample\IncomingFiles\"
Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(RFN), FtpWebRequest)
FTP.Credentials = New NetworkCredential("ftpdatasandyalexander", "Y$m0z1k30")
FTP.KeepAlive = False
FTP.UseBinary = True
FTP.Method = WebRequestMethods.Ftp.DownloadFile
Using FtpResponse As FtpWebResponse = CType(FTP.GetResponse, FtpWebResponse)
Using ResponseStream As IO.Stream = FtpResponse.GetResponseStream
Using fs As New IO.FileStream(LFN, FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = ResponseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
ResponseStream.Close()
fs.Flush()
fs.Close()
'Log("")
End Using
ResponseStream.Close()
MessageBox.Show("File Downloaded!")
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub Import_File_To_SQL()
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Sample\IncomingFiles\Test.csv;Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False")
ExcelConnection.Open()
Dim expr As String = "Select * From [sheet$]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "Data Source=MMSQL1;Initial Catalog=dbname; User Id=user; Password=pass;"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "tDHL_SortCode"
Try
objDR = objCmdSelect.ExecuteReader
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
MessageBox.Show("File Imported!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
Private Sub btndownload_Click(sender As Object, e As EventArgs) Handles btndownload.Click
Call Get_File_From_FTP()
Call Import_File_To_SQL()
End Sub
结束班
答案 0 :(得分:0)
您正尝试将文件下载到目录中而不提供文件名。问题出在这里的代码:
Dim LFN = "C:\Sample\IncomingFiles\"
它应该是文件路径而不是目录路径。
尝试
Dim LFN = "C:\Sample\IncomingFiles\Test.csv"
答案 1 :(得分:0)
这是我为解决问题所写的代码
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form1
Sub Get_File_From_FTP()
Dim buffer(1023) As Byte '
Dim bytesIn As Integer '
Dim totalBytesIn As Integer '
Dim output As IO.Stream '
Try
Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://something.com/Sort_Codes/" & "sample.csv"), System.Net.FtpWebRequest)
FTPRequest.Credentials = New System.Net.NetworkCredential("user", "pass")
FTPRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
output = System.IO.File.Create("C:\Test\IncomingFiles\Test.csv")
bytesIn = 1
Do Until bytesIn < 1
bytesIn = stream.Read(buffer, 0, 1024)
If bytesIn > 0 Then
output.Write(buffer, 0, bytesIn)
totalBytesIn += bytesIn
Label2.Text = totalBytesIn.ToString + " Bytes Downloaded"
Application.DoEvents()
End If
Loop
output.Close()
stream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub Import_File_To_SQL()
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "C:\Test\IncomingFiles" & ";" & _
"Extended Properties=""Text;HDR=YES;IMEX=1;""")
ExcelConnection.Open()
Dim expr As String = "Select * From [Test.csv]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "Data Source=server;Initial Catalog=dbname; User Id=user; Password=pass;"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "tDHL_SortCode"
Try
objDR = objCmdSelect.ExecuteReader
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
MessageBox.Show("File Imported!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
Private Sub btndownload_Click(sender As Object, e As EventArgs) Handles btndownload.Click
Call Get_File_From_FTP()
Call Import_File_To_SQL()
End Sub
结束班