zoukankan      html  css  js  c++  java
  • C# CSV Generic T

    This artice will write the main step to export generic data via csv with complete code and step by step.

    1.Down load EntityFramework

    Install-package entityframework -v 6.2.0

    2.Add EF data model with only one table for simplicity.Take the AdventureWorks2017.Sales.SalesOrderDetail for example.

    3.Override the ToString() method of the  newly added data model  cs file. 

    public override string ToString()
    {
    string formatMsg = SalesOrderID + "," + SalesOrderDetailID + "," + CarrierTrackingNumber + "," +
    OrderQty + "," + ProductID + "," + SpecialOfferID + "," + UnitPrice + "," + UnitPriceDiscount + "," +
    LineTotal + "," + rowguid + "," + ModifiedDate + Environment.NewLine;
    return formatMsg;
    }

    4.To be exact and precise,I will add StopWatch to log the cost in milliseconds.

    The full code as below.

    So far in my own pc, I can test as much as 3.8 million rows data and cost about 25 seconds as the screenshot illustrated.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Diagnostics;

    namespace ConsoleApp322
    {
    class Program
    {
    static string cvsFileFullName = Directory.GetCurrentDirectory() + "\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".csv";
    static string fullLogFileName = Directory.GetCurrentDirectory() + "\" + DateTime.Now.ToString("yyyyMMdd") + "log.txt";
    static Stopwatch stopWatch = new Stopwatch();
    static int exportNumber = 0;
    static void Main(string[] args)
    {
    ExportTToCVS();
    }

    static void ExportTToCVS()
    {
    using (AdventureWorks2017Entities db = new AdventureWorks2017Entities())
    {
    List<SalesOrderDetail> orderList = db.SalesOrderDetails.ToList();
    orderList.AddRange(orderList);
    orderList.AddRange(orderList);
    orderList.AddRange(orderList);
    orderList.AddRange(orderList);
    orderList.AddRange(orderList);
    orderList.AddRange(orderList);
    ExportListTToCVS<SalesOrderDetail>(orderList);
    }
    }

    static void ExportListTToCVS<T>(List<T> dataList)
    {

    if(dataList!=null && dataList.Any())
    {
    stopWatch.Start();
    exportNumber = dataList.Count;
    StringBuilder orderBuilder = new StringBuilder();
    var firstRowData = dataList.FirstOrDefault();
    var orderProps = firstRowData.GetType().GetProperties().ToList();
    orderBuilder.AppendLine(string.Join(",", orderProps.Select(x=>x.Name).ToArray()));
    foreach(var dl in dataList)
    {
    orderBuilder.Append(dl.ToString());
    }

    using (StreamWriter streamWriter = new StreamWriter(cvsFileFullName))
    {
    streamWriter.WriteLine(orderBuilder.ToString());
    }

    stopWatch.Stop();
    Process proc = Process.GetCurrentProcess();
    long exportMemory = proc.PrivateMemorySize64;
    string exportMsg = $"File path:{cvsFileFullName},export number:{exportNumber}," +
    $"cost: {stopWatch.ElapsedMilliseconds} milliseconds,memory:{exportMemory}";
    File.AppendAllText(fullLogFileName, exportMsg + Environment.NewLine);
    }
    }
    }
    }

  • 相关阅读:
    PHP中逻辑运算符and/or与||/&&的一个坑
    PHP usort 使用用户自定义的比较函数对数组中的值进行排序
    php编写TCP服务端和客户端程序
    Redis系列-php怎么通过redis扩展使用redis
    国内镜像源收集
    双通道内存技术简介
    收集些日本的VPS
    建站相关关键词快速普及
    bash 的漏洞,你们中招了吗?
    戴维·卡梅伦(David William Donald Cameron)经典语录
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10344396.html
Copyright © 2011-2022 走看看