简体   繁体   English

无法从SQL Server和C#应用程序在csv中下载具有80列的6到700万条记录

[英]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.

相关问题 如何在C#Web应用程序中将SQL Server表中超过100万行导出到CSV? - How to export more than 1 million rows from SQL Server table to CSV in C# web app? 从C#应用程序上载和下载SQL Server上的文件 - Upload and download files on SQL Server from a C# application 从 C# 加速将 300 万行插入 SQL 服务器 - Speed up inserting 3 million rows into SQL Server from C# 从SQL Server选择百万条记录 - Selecting million records from SQL Server C#Web API SQL Server 2012:负载测试下的错误(超过1400万条记录) - C# Web API SQL Server 2012: Errors Under Load Test (14 million+ Records) C#/ SQL Server 2016 - 更新大约具有哈希值的1亿条记录 - C# / SQL Server 2016 - Updating Approx. 100 million records with hash value 从c#中的csv获取csv数据到SQL Server中的记录的最佳方法? - Best way to get csv data from sting in C# to records in SQL Server? webrequest c#百万条记录 - webrequest c# million records 从.net c#应用程序循环正确有效地更新sql server db表中的数百条记录 - Properly and efficiently update hundreds of records in sql server db table from .net c# application loop 使用C#列出超过1000万条Oracle记录 - Listing more than 10 million records from Oracle With C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM