C# CSV Generic T
2021-06-16 21:05
标签:name cti int reading type alt order line one 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() 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; namespace ConsoleApp322 static void ExportTToCVS() static void ExportListTToCVS if(dataList!=null && dataList.Any()) using (StreamWriter streamWriter = new StreamWriter(cvsFileFullName)) stopWatch.Stop(); C# CSV Generic T 标签:name cti int reading type alt order line one 原文地址:https://www.cnblogs.com/Fred1987/p/10344396.html
{
string formatMsg = SalesOrderID + "," + SalesOrderDetailID + "," + CarrierTrackingNumber + "," +
OrderQty + "," + ProductID + "," + SpecialOfferID + "," + UnitPrice + "," + UnitPriceDiscount + "," +
LineTotal + "," + rowguid + "," + ModifiedDate + Environment.NewLine;
return formatMsg;
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
{
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();
}
{
using (AdventureWorks2017Entities db = new AdventureWorks2017Entities())
{
List
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
ExportListTToCVS
}
}
{
{
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());
}
{
streamWriter.WriteLine(orderBuilder.ToString());
}
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);
}
}
}
}
上一篇:[转]windows 10 搭建angular开发环境
下一篇:Windows10 解决“装了 .NET Framework 4.5.2/4.6.1/4.7.1等等任何版本 或版本更高的更新”问题