我有这个代码,它使用文件夹中的文件更新SQL数据库,并在更新后删除它们,但只要在文件夹中找到空白文件,它就会给我一个异常。它给出了错误"对象引用未设置为对象的实例" 。我真正想要做的是在处理每个文件之前我需要检查每个文件是否为空白然后删除它否则处理它。这是代码:
非常感谢任何帮助。
Dim dirinfo5 As DirectoryInfo
Dim allFiles5() As FileInfo
dirinfo5 = New DirectoryInfo("E:\UPDATE\")
allFiles5 = dirinfo5.GetFiles("*.csv")
Thread.Sleep(1000)
If allFiles5.Length <> 0 Then
Try
For Each fl5 As FileInfo In allFiles5
'MsgBox(fl.FullName.ToString())
Dim con As SqlConnection = New SqlConnection(SQL_con2)
Dim sr As StreamReader = New StreamReader(fl5.FullName)
Dim line As String = sr.ReadLine
Dim value() As String = line.Split(Microsoft.VisualBasic.ChrW(44))
Dim dt As DataTable = New DataTable
Dim row As DataRow
For Each dc As String In value
dt.Columns.Add(New DataColumn(dc))
Next
While Not sr.EndOfStream
value = sr.ReadLine.Split(Microsoft.VisualBasic.ChrW(44))
If (value.Length = dt.Columns.Count) Then
row = dt.NewRow
row.ItemArray = value
dt.Rows.Add(row)
End If
End While
Dim bc As SqlBulkCopy = New SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock)
bc.DestinationTableName = "[DB].[dbo].[LData]"
bc.BatchSize = dt.Rows.Count
con.Open()
bc.WriteToServer(dt)
bc.Close()
con.Close()
sr.Close()
System.IO.File.Delete(fl5.FullName)
sr.Dispose()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If