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);
}
}
}
}
C# CSV Generic T的更多相关文章
- C#, CSV,Generic, 泛型,导出
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- .net如何向csv添加一列
using System;using System.Collections.Generic;using System.Drawing;using System.Globalization;using ...
- Reading and Writing CSV Files in C#
Introduction A common requirement is to have applications share data with other programs. Although t ...
- 根据日期 读取三个csv不留指定日期的内容 新保存一个文件
using System;using System.Collections.Generic;using System.Drawing;using System.Globalization;using ...
- C#操作CSV存取类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- C# csv 操作类
using System.Data; using System.IO; using System.Text; namespace YanZhiwei.DotNet2.Utilities.Common ...
- c# 简单文件流读写CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- 【C#】用C#通过读取数据库方式读取CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
- 快速读取csv平面文件,并导入数据库,简单小工具
using DataToDeal; using LumenWorks.Framework.IO.Csv; using Microsoft.Win32; using System; using Syst ...
随机推荐
- 深入C#并行编程(2) -- 使用线程
一.可以使用位于命名空间System.Threading中的Thrad类开启线程: //声明一个新的线程 Thread myThread = new Thread(LoopFunc); //传递一个T ...
- 2_Swift基本数据类型
数字和基本数据类型 模型数据与数字,布尔值和其他基本类型. 逻辑值 struct Bool 一个值类型实例, 取值true或者flase Bool表示Swift中的布尔值.Bool通过使用其中一个布尔 ...
- Java低配版简单的随机点名系统
import java.util.*; public class Dome{ public static void addSname(String[] students){ Scanner sc = ...
- Maven报错Please ensure you are using JDK 1.4 or above and not a JRE
在部署Jenkins项目时,用Jenkins调用服务器上脚本时,脚本在服务器上执行没有任何问题,但是在Jenkins上调用时就报错,提示mvn应该使用全路径执行,当加入jdk,jre,maven环境变 ...
- arm-linux-gcc-4.5.1安装方法
写在前面 之前写了一篇arm-linux-gcc-5.4.0的安装方法,但是后来发现5.4.0这个版本可能有些太新了,所以又找了这个4.5.1版本(低版本),由FriendlyARM(友善之臂)提供, ...
- 在宿主机上执行docker容器内部的shell或程序
为了避免反复进入docker容器内部操作,可以将一系列容器内部的指令由宿主机来操作完成. 在宿主机(作者主机为windows7)上执行centos容器(name为centos-1)中/usr目录下的“ ...
- 具名插槽 slot (二)
slot 是父组件与子组件的通信方式可以将父组件的内容显示在子组件当中或者说可以将 让你封装的组件变的更加的灵活,强壮! 在子组件中 通过为多个slot进行命名.来接受父组件中的不同内容的数据 这 ...
- pycharm 远程连接
1.连接远程项目 2.连接远程服务器
- JMeter面试题
1.Jmeter怎么录制脚本,怎么过滤,线程组有哪些内容? jmeter可以使用第三方的录制工具(badboy)或者使用jmeter自带的HTTP代理服务器录制脚本功能 jmeter录制原理:通过ht ...
- (day48)Bootstrap、Adminlte框架、sweetalert
目录 Bootstrap框架官网 Adminlte框架官网 sweetalert 一.基础 二.布局 三.组件 四.插件 Bootstrap框架官网 Adminlte框架官网 sweetalert g ...