C # comes with a compression class method for exporting a database table to a CSV compressed file

Source: Internet
Author: User
Tags ziparchive

Original: C # self-compacting class method for exporting database tables to CSV compressed files

When exporting a large number of CSV data, often large size, the use of C # comes with the compression class, you can easily implement this function, and compression ratio is very high, this method in my open Source Tool Datapie has been tested in practice. My previous Blog "full-featured, efficient first-class free open source database import and Export tools (C # Development, support SQL Server, SQLite, access three kinds of database), monthly take this processing data 5G above the tool's full source, there is the need for students to see."

In. NET 4.5, you can easily create a zip file that first requires the introduction of System.IO.Compression.dll, System.IO.Compression.FileSystem.dll two files. Where the Ziparchive class represents some compressed files that use the Zip file format. The Ziparchiveentry class represents a single ziparchive. Ziparchive typically contain one or more ziparchiveentry instances.

The main code for the export of CSV file compression in Datapie is as follows:

usingSystem;usingSystem.Linq;usingSystem.Text;usingSystem.IO.Compression;usingSystem.Data;usingSystem.Diagnostics;usingSystem.IO;namespacedatapie.core{ Public classDbtozip { Public Static intDatareadertozip (String zipfilename, IDataReader Reader,stringtablename) {Stopwatch Watch=stopwatch.startnew (); Watch.            Start (); using(FileStream fsoutput =NewFileStream (Zipfilename, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {                using(Ziparchive archive =Newziparchive (Fsoutput, ziparchivemode.update)) {Ziparchiveentry Readmeentry= Archive. Createentry (tablename +". csv"); using(StreamWriter writer =NewStreamWriter (Readmeentry.open (), Encoding.UTF8)) {                         for(inti =0; I < reader. FieldCount; i++)                        {                            if(I >0) writer. Write (','); Writer. Write (reader.                        GetName (i)); } writer.                        Write (Environment.NewLine);  while(reader. Read ()) { for(inti =0; I < reader. FieldCount; i++)                            {                                if(I >0) writer. Write (','); String v=Reader[i].                                ToString (); if(V.contains (',') || V.contains ('\ n') || V.contains ('\ r') || V.contains ('"') {writer. Write ('"'); Writer. Write (V.replace ("\"","\"\"")); Writer. Write ('"'); }                                Else{writer.                                Write (v); }} writer.                        Write (Environment.NewLine); }}}} watch.            Stop (); returnConvert.ToInt32 (watch. Elapsedmilliseconds/ +); }    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.