【问题标题】:Out of Memory exception while uploading big excel file through window service通过窗口服务上传大 excel 文件时出现内存不足异常
【发布时间】:2014-06-17 06:56:55
【问题描述】:

我的申请面临一个重大问题。在通过 oledb.jet 12.0 提供程序读取一个包含超过 90 万条记录的大型 Excel 文件时,并在一段时间后系统通过我的异常消息将其从返回值填充到数据集中。

我非常努力地想找到一些解决方案,但直到现在都没有运气。

如果有人对此有相同的想法,请帮助我解决这个问题。

代码:

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

【问题讨论】:

  • 你能发一些代码吗
  • ...不要尝试一次处理所有记录?
  • +1 到@PeterRitchie,但只是出于兴趣,当它崩溃时你使用了多少内存?如果您正在运行 32 位进程,则只能使用 2GB...
  • 您可以在 32 位进程中获得超过 2GB 的内存,但这并不是一个真正有效的设计决策——您应该设计一些能够容纳大于可用内存的数据集的东西。
  • @PeterRitchie:感谢您的回复。但是您能否提供一些示例代码 sn-p 将我的 excel 读取过程划分为数据读取块。并在最后制作一个完整的累积表。

标签: .net sqlbulkcopy


【解决方案1】:

我做的更正如下:

  1. 我只是将数据读取过程从 excel 更改为 Datatable 块(100000 条记录)。
  2. 在每个读取进程线程上,我使用 SQLBulkCopy 将数据传递到目标表。

  3. 并在每次循环上传(100000条记录)后清除数据表对象中的行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 2018-08-24
    相关资源
    最近更新 更多