当前位置 : 主页 > 编程语言 > c语言 >

填充数据表的.Net System.OutOfMemoryException

来源:互联网 收集:自由互联 发布时间:2021-06-24
我需要从.dbf文件中提取数据并将其转换为xml.我写了一个例程,它做得很好.但是现在我们遇到了非常大的.dbf文件 – 比如2GB.此代码会对这些文件抛出OutOfMemoryException. Public Function GetData(
我需要从.dbf文件中提取数据并将其转换为xml.我写了一个例程,它做得很好.但是现在我们遇到了非常大的.dbf文件 – 比如2GB.此代码会对这些文件抛出OutOfMemoryException.

Public Function GetData() As DataTable
    Dim dt As New DataTable(Name)
    Dim sqlcommand As String= "Select * From MyTable"
    Dim cn As New OleDbConnection(myconnectionstring)

    Try
        cn.Open()
        Dim cmd As New OleDbCommand(sqlcommand, cn)
        dt.Load(cmd.ExecuteReader())
    Catch ex As Exception
        Throw ex
    Finally
        dt.Dispose()
        cn.Close()
        cn.Dispose()
    End Try
    Return dt

问题是 – 如果我在我的计算机上通过Visual Studio在调试模式下针对相同的2GB .dbf文件运行相同的代码,则不会抛出任何异常.这几乎就像Visual Studio以不同的方式管理内存而应用程序单独执行.

反正有没有解决内存问题?我尝试过使用类似结果的DataAdapter.我在Visual Studio预期/设计中看到的这种行为是什么?

数据表位于内存中,因此在大文件上会失败,或者根据文件的大小变慢.

您需要使用SqlDataReader按记录读取数据记录,并使用XmlWriter创建XML文件.

像这样的东西(未检查代码)

Public Sub WriteToXml(Dim xmlFileName As String, Dim connectionString)
    Dim writer As XmlWriter
    writer = XmlWriter.Create(xmlFileName)
    Dim commandText As String= "Select * From MyTable"
    Dim connection As New OleDbConnection(connectionString)

    Try
        connection.Open()
        Dim command As New OleDbCommand(commandText, connection)
        Dim reader As SqlDataReader
        reader = myCommand.ExecuteReader()

        While reader.Read()             
            write.WriteRaw("xml")
        End While
    Catch ex As Exception
        Throw ex
    Finally        
        connection.Close()
        connection.Dispose()
    End Try
End Sub
网友评论