我試圖將製表符分隔文件導入到表中。避免「文件結束」錯誤
問題是,SOMETIMES,該文件將包含一個含有兩個「空值」的尷尬記錄,並導致我的程序拋出「意外的文件結束」。
例如,每個記錄將有20個字段。但最後一條記錄只有兩個字段(兩個空值),因此也就是意外的EOF。
當前我正在使用StreamReader
。
我試過計算行數,並告訴bcp在「幻像零位」之前停止讀取,但StreamReader
由於「幻像零位」而得到不正確的行數。
我試過下面的代碼來擺脫所有僞代碼(代碼從網上借用)。但它只是用空格替換字段(我希望不留行的結果)。
Public Sub RemoveBlankRowsFromCVSFile2(ByVal filepath As String)
If filepath = DBNull.Value.ToString() Or filepath.Length = 0 Then Throw New ArgumentNullException("filepath")
If (File.Exists(filepath) = False) Then Throw New FileNotFoundException("Could not find CSV file.", filepath)
Dim tempFile As String = Path.GetTempFileName()
Using reader As New StreamReader(filepath)
Using writer As New StreamWriter(tempFile)
Dim line As String = Nothing
line = reader.ReadLine()
While Not line Is Nothing
If Not line.Equals(" ") Then writer.WriteLine(line)
line = reader.ReadLine()
End While
End Using
End Using
File.Delete(filepath)
File.Move(tempFile, filepath)
End Sub
我試過使用SSIS,但它遇到了EOF意外的錯誤。
我在做什麼錯?