问题描述
我正在通过SQL将大约60万行记录提取到DataGridView中,并按日期将它们全部分割成文件。
我尝试了几种方法来执行此操作,尽管我找到了一些方法使其不会崩溃并安全可靠地执行,但速度非常慢。
但是,我发现最快的方法只是将日期仅从“日期”列中剪除,例如将2020/06/01 13:50:43转换为0601,并将其用作文件名,然后将文本追加到该文件中。 (与在临时对象中创建所有行并一次全部写入等相反)。
速度很快,一切都很好,但是写了大概30个之后呢?在大约150个文件中,它崩溃了。这是非常随机的,我尝试运行几次。几秒钟或半分钟后它可能会崩溃。因此,我认为这是由于尝试如此快速地多次写入而导致自身跳闸所致。
我尝试多次更改写入方法,确保使用Using块以及尝试文件共享。
这是我的代码:
Private Sub btnExec_Click(sender As Object,e As EventArgs) Handles btnExec.Click
Static start_time As DateTime
Static stop_time As DateTime
Dim elapsed_time As TimeSpan
start_time = Now
If IsNumeric(TextBox1.Text) Then columnNumber = CInt(TextBox1.Text) - 1
Dim dgw = Form1.DataGridView1
Dim curcell As String = ""
Dim ToWrite As String = ""
Dim Written As Integer = 0
Try
ProgressBar1.Maximum = dgw.Rows.Count
For i = 0 To dgw.Rows.Count - 1
'For each row
If IsDBNull(dgw.Rows(i).Cells(columnNumber).Value.ToString()) Then
curcell = "" 'if null replace with a blank value
Else
curcell = CStr(dgw.Rows(i).Cells(columnNumber).Value)
End If
'RB FORMATTING
If RadioButton1.Checked = True Then '2020/01/01 00:00:00
'2020/06/16 19:38:33
curcell = curcell.Remove(0,5)
curcell = curcell.Remove(curcell.Length - 9,9)
curcell = curcell.Replace("/","")
'0616
End If
'Now we have 0616 in a string which is the date of the current row
Dim currow As String = ""
For Each c As DataGridViewCell In dgw.Rows(i).Cells
If IsDBNull(c.Value) Then
currow += Chr(34) + "" + Chr(34) + ","
Else
currow += Chr(34) + CStr(c.Value) + Chr(34) + ","
End If
Next
currow = currow.Remove(currow.Length - 1,1) 'Remove last comma
'Now we have the full row to print as well as the date in a string
Dim FILENAME As String = TextBox2.Text + "\" + curcell + ".csv"
' Using fs As FileStream = File.OpenWrite(FILENAME)
' AddText(fs,currow + vbCrLf)
' End Using
Dim fs As FileStream = New FileStream(FILENAME,FileMode.OpenOrCreate,FileAccess.Write)
Dim w As StreamWriter = New StreamWriter(fs)
w.BaseStream.Seek(0,SeekOrigin.[End])
w.Write(currow + vbCrLf)
w.Close()
'Using writer As StreamWriter = File.AppendText(FILENAME)
'writer.WriteLine(currow)
' End Using
'My.Computer.FileSystem.WriteAllText(FILENAME,currow + vbCrLf,1) 'Gets used by its own process error some times
Written += 1
Next 'Next row
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Log("Wrote " + CStr(Written) + " lines in " + elapsed_time.TotalSeconds.ToString("0.00" & " seconds."))
Catch ex As Exception
Log(ex.ToString())
End Try
End Sub
(我知道这是非常凌乱的atm,我一直在尝试很多事情还需要清理)
这是错误:
System.IO.IOException: The process cannot access the file 'C:\Users\username\Desktop\TEST2\0416.csv' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath)
at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)
at System.IO.FileStream..ctor(String path,FileAccess access)
at TicketsSQL.frmSplitFiles.btnExec_Click(Object sender,EventArgs e) in C:\Users\username\source\repos\TicketsSQL\TicketsSQL\frmSplitFiles.vb:line 63
除了当前在代码中尝试过的方法外,我还注释了另外两种方法。我尝试了以下三种方法:
Dim fs As FileStream = New FileStream(FILENAME,SeekOrigin.[End])
w.Write(currow + vbCrLf)
w.Close()
Using writer As StreamWriter = File.AppendText(FILENAME)
writer.WriteLine(currow)
End Using
My.Computer.FileSystem.WriteAllText(FILENAME,1)
似乎无论我如何尝试使用已经打开的文件来阻止它,甚至允许它这样做,无论如何,它都会跳闸。
有人可以解决此问题吗?现在已经呆了好几个小时了...谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)