我在应用程序中遇到了一个主要问题。当通过oledb.jet 12.0提供程序读取一个包含90多万条记录的大型excel文件,并在一段时间后将其从返回值填充到数据集中时,系统通过我发现了一个异常,消息为out of memory。
我很努力地想找到一些解决办法,但到目前为止没有任何运气。
如果有人对此有一些想法,请在这个问题上帮助我。
代码:
MyConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FileSource & ";Extended Properties=""Excel 12.0;IMEX=1""")
MyConn.Open()
'get the table schema information to retrive sheet name
Dim schemaTable As DataTable = MyConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
For Each dtrow As DataRow In schemaTable.Rows
Dim iUploadStatus As Integer = 0
sheetName = String.Empty
sheet = dtrow("TABLE_NAME").ToString()
'to skip the duplicate sheet being fetched in the schema table.
If sheet.Contains("_xlnm#_FilterDatabase") Or sheet.Contains("_FilterDatabase") Then
Continue For
End If
MyCommand = New OleDbDataAdapter _
("select * from [" + sheet + "] ", MyConn)
MyCommand.TableMappings.Add("Table", "TestTable")
DtSet = New DataSet
MyCommand.Fill(DtSet)
Using destinationConnection As New SqlConnection(Conn)
' open the connection
destinationConnection.Open()
Using bulkCopy As New SqlBulkCopy(Conn)
' column mappings
bulkCopy.ColumnMappings.Add(P1, ColProdNum)
bulkCopy.ColumnMappings.Add(P2, ColProdDesc)
bulkCopy.ColumnMappings.Add(P3, ColListPrice)
bulkCopy.ColumnMappings.Add(P4, ColNetPrice)
bulkCopy.BatchSize = 2000
bulkCopy.NotifyAfter = 2000
bulkCopy.DestinationTableName = "tabDestination"
bulkCopy.WriteToServer(DtSet.Tables(0))
End Using
End Using
我所做的更正如下:
- 我只是将数据读取过程从excel改为数据表(由100000条记录组成(
-
在每个读取进程线程上,我都使用SQLBulkCopy将数据传递到目标表。
-
在每次循环上传(100000条记录(之后,清除数据表对象中的行。