Posts

SQL Server Numbers table

if not exists ( select 1 from information_schema . schemata where schema_name = 'util' ) begin exec sp_executesql N 'create schema [util];' ; end ; go create table util . Numbers ( Num int not null , constraint PK_util_Numbers primary key ( Num ) ) ; go declare @MaxValue int = 1000000 ; while ( select count ( 1 ) from util . Numbers ) < @MaxValue begin declare @n int = @MaxValue - ( select count ( 1 ) from util . Numbers ) ; declare @offset int = isnull ( ( select max ( Num ) from util . Numbers ) , 0 ) ; insert util . Numbers select top ( @n ) row_number ( ) over ( order by o1 . [ object_id ] ) + @offset from sys . objects o1 cross join sys . objects o2 end ; select * from util . Numbers order by Num ; go

Median in .NET

dotnet add package MathNet.Numerics https://numerics.mathdotnet.com/ MathNet . Numerics . Statistics . Statistics . Median ( nums )

Convert UTC to local time in SQL Server

 Thanks to this StackOverflow answer : convert ( datetime , switchoffset ( convert ( datetimeoffset , MyTable . UtcColumn ) , datename ( TzOffset , sysdatetimeoffset ( ) ) ) ) as ColumnInLocalTime

TypeScript settings in csproj

These are the settings I put in the csproj file for TypeScript – it sets values to convert to ES5, takes all TypeScript files and compiles to individual JavaScript files in the /Scripts/js directory. From there, I’ll bundle them into a single site.js file inside of wwwroot/js using BunderMinifier. < PropertyGroup > < TypeScriptTarget > ES5 </ TypeScriptTarget > < TypeScriptJSXEmit > None </ TypeScriptJSXEmit > < TypeScriptModuleKind > CommonJS </ TypeScriptModuleKind > < TypeScriptCompileOnSaveEnabled > True </ TypeScriptCompileOnSaveEnabled > < TypeScriptNoImplicitAny > True </ TypeScriptNoImplicitAny > < TypeScriptNoExplicitAny > False </ TypeScriptNoExplicitAny > < TypeScriptRemoveComments > False </ TypeScriptRemoveComments > < TypeScriptOutFile > </ TypeScriptOutFile > < TypeScriptOutDir > Scripts/js </ TypeScriptOutDir > ...

Add response compression in .NET Core

services . AddResponseCompression ( options = > options . Providers . Add < GzipCompressionProvider > ( ) ) ; app . UseResponseCompression ( ) ;

Force TLS 1.2 with WebClient

In .NET Framework 4.0+, use  this StackOverflow answer to disable TLS 1.0 and force 1.1 or 1.2: ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls11 | SecurityProtocolType . Tls12 ; In prior .NET Framework versions, you can force it without the enum value: public const SslProtocols _Tls12 = ( SslProtocols ) 0x00000C00 ; public const SecurityProtocolType Tls12 = ( SecurityProtocolType ) _Tls12 ; ServicePointManager . SecurityProtocol = Tls12 ;

Catching SQL exceptions in .NET

Here’s one way of handling SQL errors from .NET: For expected errors, like data validation, raise an error with a specific error code and state. Unexpected errors will still blow up with whatever SQL Server’s error message is: create procedure dbo . TestProc ( @customError bit , @sqlError bit ) as begin begin transaction ; begin try if @customError = 1 begin raiserror ( 'This is my custom error.' , 16 , 99 ) ; return ; end ; if @sqlError = 1 begin exec ( 'select * from SomeFakeTable' ) ; return ; end ; select 1 ; commit transaction ; end try begin catch if @@trancount > 0 rollback transaction ; declare @errorMessage nvarchar ( 4000 ) = error_message ( ) ; declare @errorSeverity int = error_severity ( ) ; declare @errorState int = e...