Posts

Showing posts from February, 2021

Mustache in .NET

Image
Mustache is a really simple templating engine, and a good choice for building HTML. Here's an example using the Stubble package in .NET. View code on GitHub

Update with Output in SQL Server

Image
If you're doing an update, and need information on the records you're updating, you can do it all in a single command. This video gives you an example using a CTE, where you update, and also pull data down from the records that you're updating, all in a single command. View code on GitHub

SQL Cursors

Image
Most of the time, you can use regular queries to accomplish your needs in a SQL database. There are plenty of interesting design patterns to work with sets of data, but once in awhile, you need the flexibility of a cursor. Here's an example of a straightforward read only cursor. View code on GitHub

Writing CSV with CsvHelper

Image
The CsvHelper library includes a very easy way to transform .NET objects into properly formatted CSV. To read CSV, check out my previous post . View code on GitHub

Reading CSV with CsvHelper

Image
The CsvHelper library makes it easy to read and write CSV in .NET. Exploring the examples shows you how to map a reader to a data class, through attributes or a class map, and gives you various options on transforming the data. Here's an example showing how to use it to read a CSV file. View code on GitHub

Deno Line Reader

Image
Deno is off to a good start in its path to replace NodeJS. It's secure by default, and since it was created by the creator of NodeJS, you know it's good. Here's a simple line reader project, same as my C# and NodeJS videos, just showing how to parse a text file line by line. It accesses the filesystem, so you must run it with the --allow-read option. View code on GitHub

Calculate Hash in .NET

Image
With .NET, you can calculate a file hash, or the hash of any stream, using the HashAlgorithm.ComputeHash method. Here's a sample application showing how to choose one of four algorithms and formats. To see how the SendTo functionality works, check out the previous video . View code on GitHub

SendTo in Windows

Image
You can add batch files or shortcuts to the SendTo directory in Windows, and they're immediately available when you right-click something in File Explorer. You can use this technique to do whatever you can think of for files and directories. Here are a few basic techniques for working with batch files. View code on GitHub

Log to Console Only in Dev

Image
You may want to use console.log to help you debug during development time, but you don't want those messages to appear to end users in production. You've got several ways to accomplish this - you can strip out the lines as part of your build process, or you can overwrite the console.log function entirely, or you can try the technique here. In these examples, you can still use console.log when you specifically want to write to the console, even in production, but use a separate function, console.logVerbose , to output only in development, or when you've turned verbose logging on in the browser with a query string parameter. View code on GitHub

Insert with Output in SQL Server

Image
When inserting records in SQL Server, you can output the results into a temporary or real table, with the OUTPUT clause . This can be used to return database-generated values without a secondary select, or to act similarly to a trigger, giving you the ability to do something after records get inserted. View code on GitHub

HTML to Text in ASP.NET

Image
Here's an easy way to convert HTML to only the plain text, using the NUglify library. View code on GitHub

Brotli in .NET

Image
Brotli is a compression algorithm that seems to be designed to eventually replace GZip. There are a lot of factors involved, but it seems to compress better than GZip, and is also very fast. Here and here are some links to direct comparisons. Compressing and decompressing in .NET is exactly the same as with GZip, just substituting out the classes. View code on GitHub

Add Versioning to Existing Table

Image
If you have an existing table in SQL Server, and you want to add a history table, here are some steps you can follow. If you're new to temporal tables, check out my previous post . View code on GitHub

Add Data to Temporal Table

Image
If you need to add records to an existing temporal table in SQL Server, and you need to manipulate the row start column, then you'll have to do some extra work. If you're new to temporal tables, check out my previous post . View code on GitHub

Alter Temporal Table

Image
Temporal tables are a little trickier to work with than regular tables in SQL Server. When altering the table structure, you have to first disable versioning. If you're new to temporal tables, check out my previous post . View code on GitHub

Temporal Tables

Image
In the past, you needed triggers and custom tables set up to keep a history of changes in table data - or you had to manually write the history in stored procs. With temporal tables, SQL Server does all the work for you. There are several ways to get this going, other than what's in the video. This is a fantastic read on the subject where you can see other ways to set things up. View code on GitHub

RSA in .NET

Image
Cryptography, and especially public key cryptography, is a complicated and difficult topic. I recommend digging into the docs and blogs from trusted security experts. To get you started, here's an example showing creating keys, encrypting, decrypting, and signing a message. Protecting your private keys is probably the most important piece of the process, so make sure you do your research on that before jumping into this in real life. View code on GitHub

Case sensitive strings in SQL Server

Image
99 times out of 100, I've wanted case-insensitive strings in SQL Server. It's good for manually querying things, it's good for searchable data like names or email addresses, and it's generally accepted as the standard. However, once in awhile, you are going to want something case-sensitive. This would be for things like storing base-64 encoded values or URL shortener codes. Here's how to do that. For more info on collation, check out the docs View code on GitHub

Add EXIF Date to JPEG in .NET

Image
EXIF is an incredibly large standard for digital photos. My goal here is just to add or modify the Date Taken property to a photo so it'll appear in Google Photos or Windows with the date I want. Here's the standard: EXIF Standard , and a list of standard tags . View code on GitHub

SQL Server GUID as Primary Key

Image
There are a few use cases for using a GUID as a primary key on a table. If you fall into one of those categories, make sure you are optimizing storage by properly defining an incrementing clustered index. You can solve some problems by using NEWSEQUENTIALID() to generate your key, but that actually can make things worse. For example, this forces you to use a database-generated GUID, which means your app won't know the ID until the insert is done. It also means you can't migrate your records from one database to another without paging issues. Here's a good read on the subject: GUIDs as PRIMARY KEYs and/or the clustering key View code on GitHub

C# String Interpolation

Image
C# string interpolation gives you a cleaner way to do String.Format. See the docs here . View code on GitHub

Base 64 in SQL Server

Image
Base 64 encoding is the process of converting bytes (8 bits) to readable, printable characters. To do this, 64 possible characters are used (6 bits), and every 3 bytes translates into 4 characters. SQL Server generally shouldn’t need to do this. - storing binary in the database is just fine - but if you need it, here’s one way to do it. There’s another XML function you could do, something like xs:base64Binary - or you could also use a CLR function - but this is a pretty easy way to go. View code on GitHub