Skip to content

Instantly share code, notes, and snippets.

View demius's full-sized avatar

Alexis vd Merwe demius

View GitHub Profile
@demius
demius / SafeSqlStoredProcedure.sql
Created August 20, 2021 02:10
SQL stored procedure template with transactions and exception handling
/*
A nice template for SQL TRY-CATCH with transactions
Taken from this SO discussion thread: https://stackoverflow.com/questions/7488149/sql-transaction-error-the-current-transaction-cannot-be-committed-and-cannot-su
*/
create procedure [usp_my_procedure_name]
as
begin
set nocount on;
declare @trancount int;
set @trancount = @@trancount;
@demius
demius / ComputeBacpacHash.ps1
Created December 6, 2019 08:47
Compute BACPAC hash
$modelXmlPath = Read-Host "model.xml file path"
$hasher = [System.Security.Cryptography.HashAlgorithm]::Create("System.Security.Cryptography.SHA256CryptoServiceProvider")
$fileStream = new-object System.IO.FileStream ` -ArgumentList @($modelXmlPath, [System.IO.FileMode]::Open)
$hash = $hasher.ComputeHash($fileStream)
$hashString = ""
Foreach ($b in $hash) { $hashString += $b.ToString("X2") }
$fileStream.Close()
$hashString
@demius
demius / Test-IsInElevatedMode.ps1
Created July 11, 2018 09:32
Powershell: Check for elevated permissions
# Check whether we are running in elevated mode
function Test-IsInElevatedMode {
<#
.DESCRIPTION
Checks to see whether the current Powershell prompt is running in elevated mode
.EXAMPLE
Test-IsInElevatedMode
.OUTPUTS
@demius
demius / UriInterpolator
Last active June 25, 2018 07:16
A dynamic URI interpolator
public static class UriInterpolator
{
/// <summary>
/// Dynamically produces a parameterized path using the provided dynamic <paramref name="parameters"/> object
/// </summary>
/// <param name="path">The path with placeholders in the format of {paramName}</param>
/// <param name="parameters">A dynamic object representing parameter values to be interpolated into the <paramref name="path"/></param>
public static string BuildQueryPath(string path, dynamic parameters)
{
if (parameters == null)
@demius
demius / Time.cs
Last active October 8, 2019 06:48
C# Time class
/// <summary>
/// A simple representation of time
/// </summary>
public sealed class Time : IEquatable<Time>
{
/// <summary>
/// Gets the hour
/// </summary>
public int Hour { get; }
@demius
demius / Win32FileInspector.cs
Last active May 8, 2018 06:46
C# Win32 lock checking
internal static class FileInspector
{
[DllImport("kernel32.dll")]
private static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName, System.UInt32 dwDesiredAccess, System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition, System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
private static readonly uint GENERIC_WRITE = 0x40000000;
private static readonly uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
@demius
demius / Global.asax.cs
Created September 1, 2016 09:19
Extensionless WCF service hosting using ASP.NET Routing
using System;
using System.ServiceModel.Activation;
using System.Web.Routing;
namespace ACME.Services
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
@demius
demius / knockout.dragdrop.js
Last active August 29, 2015 14:06
KnockoutJS drag-drop binding extension
ko.bindingHandlers.drag = {
_dragging: false,
init: function (element, valueAccessor, allBindingsAccessor, data, context) {
$(element).attr('draggable', 'true');
$(element).on('dragstart', function(e) {
// Payload (assumed to be a viewmodel) is converted to a plain JSON object
var d = ko.unwrap(valueAccessor());
e.originalEvent.dataTransfer.setData("application/json", ko.toJSON(d));
this._dragging = true;