我试图将制表符分隔文件导入到表中。避免“文件结束”错误
问题是,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意外的错误。
我在做什么错?