title: "Out Of Memory" exception while exporting from SQL Server to Excel, using EPPlus library
body: <p>I'm using the EPPlus .NET library in order to export data from SQL Server to an Excel file.</p>
  
  <p>I'm using the <code>SqlConnection</code> class to read the data. For every row of the <code>SqlDataReader</code> cursor, I iterate through all the excel cells for the corresponding row, and enter the data from the reader.</p>
  
  <p>The issue is that I'm getting an "out of memory" error when im using this function for large tables. I need a method to create some kind of a buffer inside the <code>Read</code> CURSOR.</p>
  
  <p>A concise code example:</p>
  
  <pre><code>Dim sqlConnection As SqlConnection = New SqlConnection()
  sqlConnection.ConnectionString = sqlConnectionString.ConnectionString 'connectionstring built before
  
  Dim query As SqlCommand = New SqlCommand(query...)
  
  Dim newFileStream As New FileStream("c:\junk\test.xlsx", System.IO.FileMode.Create,System.IO.FileAccess.ReadWrite)
  
  Using excelApp As New ExcelPackage(newFileStream)
      sqlConnection.Open()
      Dim sqlReader As SqlDataReader = query.ExecuteReader()
  
      Dim numOfColumns As Byte = sqlReader.FieldCount()
      Dim rowNumber As Integer = 1
  
      While sqlReader.Read()
          Dim currentColumn As Byte
  
          For currentColumn = 1 To numOfColumns
              ws.Cells(rowNumber,currentColumn).Value = sqlReader.Item(currentColumn - 1)
          Next
       rowNumber += 1             
      End While
  
      excelApp.Save()
  End Using
  
  newFileStream.Close()
  </code></pre>