Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Iam fetching 25 lakh (250,000) records from my sql and the records have 5 columns in the table..
the fetched records are imported to CSV file...
for 6 or 7 lakhs records its fetching and exported to csv file..

but for more than 20 lakhs records its throwing the system out of memory exception..

im using this code to export the records to CSV...

C#
public static void ExportDataTableToCSV(DataTable dt, string FileName, string Heading, HttpResponse objResponse)
      {
          System.Text.StringBuilder sb = new System.Text.StringBuilder();
          sb.AppendLine(Heading);
          string[] columnNames = dt.Columns.Cast<DataColumn>().
                                            Select(column => column.ColumnName).
                                            ToArray();
          sb.AppendLine(string.Join(",", columnNames));

          foreach (DataRow row in dt.Rows)
          {
              string[] fields = row.ItemArray.Select(field => field.ToString().Replace(',',' ')).
                                              ToArray();
              sb.AppendLine(string.Join(",", fields));
          }
          objResponse.Write(sb.ToString());
          objResponse.ContentType = "application/CSV";
          objResponse.AddHeader("content-disposition", "attachment; filename="+FileName+".CSV");
          objResponse.Flush();
          objResponse.End();
      }



My issue is that im not able to fetch huge amount of records like 25 lakhs from mysql and

pls give me any other techniques to export the fetched records...

Is there any option to fill the data using json techniques or any other techniques..pls suggest me some answers...

Im importing 90 MB data from database...
Posted
Updated 7-Dec-16 1:10am
v4
Comments
BillWoodruff 24-Jan-14 2:25am    
How much memory have you got ?
Member 9700867 24-Jan-14 2:27am    
90 MB...
Member 9700867 24-Jan-14 2:37am    
90 MB and 25 lakhs record...
Mohibur Rashid 24-Jan-14 20:58pm    
In English there is no such word as Lakhs. You meant to say 2500,000
Hrishikesh 7-Dec-16 7:18am    
JUST FYI:
https://en.oxforddictionaries.com/definition/lakh

Use a StreamWriter in a 'using block, and write each row to the CSV file [^] rather than trying to fill the StringBuilder with all the rows data.
 
Share this answer
 
Why use a StringBuilder?

Instead of
sb.AppendLine(string.Join(",", fields));


Write to the response directly:
objResponse.Write(string.Join(",", fields));


Good luck!
 
Share this answer
 
Comments
Member 9700867 24-Jan-14 6:59am    
hi E.F. Nijboer...im exporting 25 lakhs records..but when i use this code only last record is exporting in the excel..thats y i used string builder..
E.F. Nijboer 24-Jan-14 11:35am    
Maybe you need to call AddHeader and ContentType first. You also don't need to use End explicitly because it is only implemented for classic asp compatibility.
E.F. Nijboer 24-Jan-14 11:40am    
You might need to use OutputStream.Write. Check this link for more info:

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.outputstream%28v=vs.110%29.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900