需要批处理大数据表并将每批数据写入文本文件-VB.Net

问题描述 投票:0回答:1

我有一个需要查询数据库并获取数据表中记录的要求。数据表有20,000条记录。我需要将这些记录按每100条记录的批次进行批处理,并将这些批处理写入单个文本文件。

到目前为止,我已经能够使用DataRow的IEnumerable对记录进行批量处理,每批100条。

我现在在将IEnumeable(Of DatRow)写入文本文件时遇到问题。

我的代码在下面:

Dim strsql = "Select * from myTable;"
Dim dt as DataTable
Using cnn as new SqlConnection(connectionString)
cnn.Open()
Using dad as new SqlAdapter(strsql ,cnn)
dad.fill(dt)
End Using
cnn.Close()
End Using 

Dim Chunk = getChunks(dt,100)
For each chunk as IEnumerable(Of DataRow) In Chunks
Dim path as String = "myFilePath"
If Not File.Exists(myFilePath) Then
  //** Here I will write my Batch into the File.
End If
Next

Public Iterator Function getChunks(byVal Tab as DataTable, byVal size as Integer) as IEnumerable (Of IEnumerable(of DataRow))

Dim chunk as List(Of DataRow) = New List(of DataRow)(size)
For Each row As DataRow in tab.Rows
chunk.Add(row)
if chunk.Count = size Then
Yield chunk
chunk = New List(of DataRow0(size)
Next
if chunk.Any() Then Yield chunk

End Function

需要您的帮助才能将DataRows的IEneumerable写入每个记录批次的文本文件。

谢谢

:)

我有一个需要查询数据库并获取数据表中记录的要求。数据表有20,000条记录。我需要将这些记录分批处理,每组100条记录,然后将其写出...

c# asp.net vb.net batch-processing
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.