简体   繁体   English

如何在C#Web应用程序中将SQL Server表中超过100万行导出到CSV?

[英]How to export more than 1 million rows from SQL Server table to CSV in C# web app?

I am trying to export a SQL Server table with 1 million rows and 45 columns to a .csv file for the user to download via the web interface but it takes so long that I eventually have to stop the process manually. 我正在尝试将具有100万行和45列的SQL Server表导出到.csv文件,以供用户通过Web界面下载,但该过程花了很长时间,最终我不得不手动停止该过程。

I use a SqlDataReader and write into the file as the reader reads to avoid memory problems. 我使用SqlDataReader并在读取器读取时将其写入文件,以避免内存问题。 The code works for small tables (less than 3k rows) but the large one keeps running and the destination file stays at 0 KB. 该代码适用于小型表(少于3k行),但大型表可继续运行,并且目标文件的大小为0 KB。

using (spContentConn) { using (var sdr = sqlcmd.ExecuteReader())
    using (CsvfileWriter)
    { 
        DataTable Tablecolumns = new DataTable();

        for (int i = 0; i < sdr.FieldCount; i++)
        {
            Tablecolumns.Columns.Add(sdr.GetName(i));
        }

        CsvfileWriter.WriteLine(string.Join("~", Tablecolumns.Columns.Cast<DataColumn>().Select(csvfile => csvfile.ColumnName)));

        while (sdr.Read())
            for (int j = Tablecolumns.Columns.Count; j > 0; j--)
            {
                if (j == 1)
                    CsvfileWriter.WriteLine("");
                else
                    CsvfileWriter.Write(sdr[Tablecolumns.Columns.Count - j].ToString() + "~");
            }
    }

I used the same answer recommended in this thread but still doesn't work. 我在此线程中使用了建议的相同答案,但仍然无法正常工作。 Please help. 请帮忙。 export large datatable data to .csv file in c# windows applications 将大型数据表数据导出到C#Windows应用程序中的.csv文件

It is not clear from the .NET documentation whether FileWriter has efficient buffering, therefore I always use a BufferedStream instead when I need to read/write large volumes of data. 从.NET文档中尚不清楚FileWriter是否具有有效的缓冲,因此当我需要读取/写入大量数据时,我总是使用BufferedStream代替。 With a stream, you would have to write byte data instead of strings, but that requires only a minor adaptation of your code. 对于流,您将不得不写字节数据而不是字符串,但这只需要对代码进行少量修改即可。

It also looks like you are reading and writing the columns of a DataTable in a loop, which would affect performance. 看起来您正在循环读取和写入DataTable的列,这会影响性能。 Since the number and order of the columns would not change during an export operation, consider using the positional index to access the column values instead. 由于在导出操作期间列的数量和顺序不会改变,因此请考虑使用位置索引来访问列值。 It would also be better to write one row at a time instead of one column at a time. 最好一次写一行而不是一次写一列。

Finally, you are using a data-reader, so that should provide the best throughput of data from your SQL Server (limited by your server and bandwidth, obviously). 最后,您使用的是数据读取器,因此应该提供来自SQL Server的最佳数据吞吐量(显然受服务器和带宽的限制)。 This would also suggest that the performance bottleneck is in the way that your data is being written to file. 这也表明性能瓶颈是将数据写入文件的方式。

For comparison, I just wrote 1,000,000 rows of 45 columns to a text file in under 60 seconds. 为了进行比较,我仅在60秒内将1,000,000行的45列写入文本文件。 Granted that my code does not read from a database, but that should still provide a good enough baseline for you. 承认我的代码没有从数据库中读取,但是仍然可以为您提供足够好的基准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM