[英]Not able to download 6 to 7 million records with 80 columns in csv from SQL Server and C# application
I need to download an extract from my application created on asp.net C#. 我需要从在asp.net C#上创建的应用程序中下载摘录。 It reads from SQL Server 2008 and tries to write it into .csv
file. 它从SQL Server 2008读取并尝试将其写入.csv
文件。 There are 6 to 7 million rows with approx 80 columns. 有6到700万行,大约80列。 The application breaks and stops responding and I'm not able to download through web browser. 应用程序中断并停止响应,我无法通过Web浏览器下载。
As an alternative I tried creating a windows service which runs on server to extract the data and create a csv file. 作为替代方案,我尝试创建Windows服务,该服务在服务器上运行以提取数据并创建csv文件。 But in case of multiple download request the server runs out of memory and the service breaks and stops working. 但是,如果有多个下载请求,则服务器内存不足,服务中断并停止工作。
Total data in table is 700 MB and to read this the server memory reaches utilization of 5 GB out 8 GB for each request. 表中的总数据为700 MB,要读取此数据,服务器内存将达到5 GB的利用率(每个请求8 GB)。 In case if there are multiple async request the memory utilization goes to 100% and service stops and file is not generated. 如果存在多个异步请求,则内存利用率将达到100%,并且服务将停止并且不会生成文件。
Currently using data table and rows as follows, need a way to download the data using C# application on request or any other means which can generate the csv file from the data given from the stored procedure. 当前如下使用数据表和行,需要一种方法,该方法可应要求使用C#应用程序下载数据,或通过其他任何可从存储过程提供的数据中生成csv文件的方式。
Thanks 谢谢
// the method to extract data
public getdata( xyz ....)
{
dbInstance = ConfigDBDAL.GetTransactionDBInstance(userDO);
DbCommand dbCommand = dbInstance.GetStoredProcCommand(PR_GET_EXTRACTS);
dbCommand.CommandTimeout = 0;
// a few parameter passing like this
DbParameter paramStartDate = new SqlParameter("@StartDate", typeof(DateTime));
paramStartDate.Value = startDate;
dbCommand.Parameters.Add(paramStartDate);
DbParameter paramStartDate = new SqlParameter("@EndDate", typeof(DateTime));
paramStartDate.Value = startDate;
dbCommand.Parameters.Add(paramStartDate);
// Creating a data table for the required 80 columns
datatable = new DataTable();
datatable.Columns.Add("COL1");
datatable.Columns.Add("COL2");
datatable.Columns.Add("COL3");
.
.
.
datatable.Columns.Add("COL80");
using (IDataReader reader = dbInstance.ExecuteReader(dbCommand))
{
DataRow newRow;
while (reader.Read())
{
row = datatable.NewRow();
row["COL1"] = reader["COL1"];
row["COL2"] = reader["COL2"];
.
.
row["COL80"] = reader["COL80"];
datatable.Rows.Add(row);
}
}
return datatable;
}
DataTable
class produces some overhead. DataTable
类产生一些开销。 Try to build pure CSV string using StringBuilder
: 尝试使用StringBuilder
构建纯CSV字符串:
var sb = new StringBuilder();
sb.AppendLine("sep=,");
sb.AppendLine("COL1,COL2,COL3,...,COL-N"); // columns
using (IDataReader reader = dbInstance.ExecuteReader(dbCommand))
{
DataRow newRow;
while (reader.Read())
{
sb.Append((string)reader["COL1"] + ",");
sb.Append((string)reader["COL2"] + ",");
sb.Append((string)reader["COL3"] + ",");
...
sb.Append((string)reader["COL-N"] + Environment.NewLine);
}
}
return sb.ToString(); // returns string representing CSV file content
You can also try this method. 您也可以尝试此方法。
public static void ExportPlantData(string channelId)
{
string query = string.Empty;
DataService dataService = new DataService();
DbCommand dataCmd = null;
DataTable contentToExport = new DataTable();
try
{
query = "SELECT * from tablename";
dataCmd = dataService.Database.GetSqlStringCommand(query);
contentToExport = dataService.ExecuteDataTable(dataCmd);
ExportToCSV(contentToExport);
}
}
public static void ExportToCSV(DataTable contentToexport)
{
StringBuilder csvData = new StringBuilder();
StringBuilder headers = new StringBuilder();
foreach (DataRow row in contentToexport.Rows)
{
headers = string.Empty;
foreach (DataColumn column in contentToexport.Columns)
{
csvData.Append(row[column].ToString() + ",");
headers.Append(column.ColumnName + ",");
}
csvData.Append("\r\n");
headers.Append("\r\n");
}
string contentToExport = headers.Append(csvData.ToString()).ToString();
string attachment = "attachment; filename=export.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.Write(contentToExport);
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.