我正在尝试将制表符分隔的文件导入表中。
问题是,有时,该文件将包含一条尴尬的记录,该记录具有两个“空值”,并导致我的程序抛出“意外的文件结尾”。
例如,每条记录将有 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 意外错误。
我究竟做错了什么?