Skip to content

Instantly share code, notes, and snippets.

View MarkPflug's full-sized avatar
🌲

Mark Pflug MarkPflug

🌲
View GitHub Profile
@MarkPflug
MarkPflug / SqlTimeZoneConvert.cs
Created January 19, 2024 17:59
SQL TZ Convert
using System.Data.SqlClient;
internal class Program
{
static void Main(string[] args)
{
var dbStr = args[0];
var srcTz = args[1];
var dstTz = args[2];
@MarkPflug
MarkPflug / example.cs
Created January 5, 2024 16:05
CsvPostgresBulkInsert
using Npgsql;
using NpgsqlTypes;
using Sylvan.Data.Csv;
using System.Collections.ObjectModel;
using System.Data.Common;
class Program
{
static async Task Main()
{
@MarkPflug
MarkPflug / CsvValidate.cs
Last active October 30, 2023 18:30
CSV Parser Validation
using CsvHelper;
using nietras.SeparatedValues;
using RecordParser.Extensions;
using Sylvan.Data;
using Sylvan.Data.Csv;
using System.Globalization;
using System.Runtime.CompilerServices;
var tw = new StringWriter();
tw.WriteLine("A,B,C,D");
This file has been truncated, but you can view the full file.
AA
AAH
AAHED
AAHING
AAHS
AAL
AALII
AALIIS
AALS
AARDVARK
@MarkPflug
MarkPflug / MacOSTextReader.cs
Last active May 4, 2023 15:41
MacOSTextReader
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
sealed class MacOSTextReader : TextReader
{
readonly TextReader inner;
public MacOSTextReader(TextReader inner)
// this is a reworking of the CsvFileResult code posted to the CSharp Reddit:
// https://www.reddit.com/r/csharp/comments/12sip6r/oom_on_custom_fileresult_streaming_from_queryable/
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
public class CSVFileResult<T> : FileResult where T : class
{
private readonly IQueryable<T> _data;
@MarkPflug
MarkPflug / Demo.cs
Last active April 14, 2023 18:14
Excel Class Data to Json
// A C# solution to the PowerShell Excel->JSON conversion described in this blog post:
// https://devblogs.microsoft.com/powershell-community/convert-specific-table-of-excel-sheet-to-json/
// Using the Sylvan data libaries.
using Sylvan.Data;
using Sylvan.Data.Excel;
if (args.Length != 3)
{
Console.WriteLine("Args: file sheet \"class name\"");
@MarkPflug
MarkPflug / CsvValidationExample.cs
Last active April 20, 2023 20:59
CSV Validation Example
// C# 11 and .NET 6+
using Sylvan.Data; // 0.2.12-B0001
using Sylvan.Data.Csv; // 1.2.7
// the schema for the csv data below.
var schema =
new Schema.Builder()
// ID is required!
.Add<int>("Id")
@MarkPflug
MarkPflug / StructCsv.cs
Created March 24, 2023 15:26
A low-allocation CSV struct writer
using System.Collections;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.Diagnostics;
var rand = new Random();
var data =
Enumerable
.Range(0, 10000000)
@MarkPflug
MarkPflug / ExcelToHtml.cs
Created November 7, 2022 16:53
Excel to HTML
using Sylvan.Data.Excel;
using var r = ExcelDataReader.Create("Data.xlsx");
r.WriteHtmlTable(Console.Out);
static class DataExtensions
{
public static long WriteHtmlTable(this System.Data.Common.DbDataReader data, TextWriter writer)
{
long count = 0;