Skip to content

Instantly share code, notes, and snippets.

View DForshner's full-sized avatar

David Forshner DForshner

View GitHub Profile
@DForshner
DForshner / KillDBConnections.sql
Last active December 11, 2015 03:48
Kill connections to a database on MS SQL 2008.
-- List all the connections to a particular database.
SELECT spid, loginame, hostname, program_name
FROM master..sysprocesses [sysprocesses]
INNER JOIN sys.sysdatabases [sysdatabases]
ON [sysprocesses].dbid = [sysdatabases].dbid
INNER JOIN sys.sysusers [sysusers]
ON [sysprocesses].uid = [sysusers].uid
WHERE [sysprocesses].dbid = DB_ID('Catalog name')
-- Kill the connection.
@DForshner
DForshner / ReadonlyMutableStructVsClass.cs
Last active December 14, 2015 15:38
Readonly mutable struct vs. class
struct MutableStruct
{
private int x;
public int Mutate()
{
this.x = this.x + 1;
return this.x;
}
}
@DForshner
DForshner / ConvertDATETIMEToUNIXTime.sql
Last active December 14, 2015 15:38
Converts DATETIME to UNIX time by finding the the difference in seconds between 1970-01-01 00:00:00 and input parameter rounded to nearest second. Valid datetime values are between: 1901-12-13 20:45:51.500 (-2,147,483,648) and 2038-01-19 03:14:07.497 (2,147,483,647)
DECLARE @dtmDATETIME DATETIME
SET @dtmDATETIME = '2009-08-09' -- '1901-12-14', '2038-01-19'
DECLARE @intResult INT;
-- NULL if outside of valid UNIX Time range (-2,147,483,648 to 2,147,483,647)
IF (@dtmDATETIME < '1901-12-14') OR (@dtmDATETIME > '2038-01-19 03:14:07.497')
SELECT NULL
ELSE
SELECT DATEDIFF(second ,25567, (DATEADD(millisecond, ROUND(DATEPART(millisecond, @dtmDATETIME),-3) - DATEPART(millisecond, @dtmDATETIME), @dtmDATETIME)) )
@DForshner
DForshner / ConvertUNIXTimeToDATETIME.sql
Created March 7, 2013 18:01
Converts UNIX time represented as the difference in seconds between 1970-01-01 00:00:00 to a datetime.
DECLARE @intUNIXTime INT
SET @intUNIXTime = 1249776000 --1249776000, 2147472000
SELECT DATEADD(second, @intUNIXTime, 25567)
@DForshner
DForshner / TableVariablesAreNotPartOfTransactions.sql
Last active December 16, 2015 02:58
Example of how table variables are not part of transactions.
-- Part 1 - Roll back a table transaction
CREATE TABLE MyTable ( Column1 VARCHAR(5), Column2 VARCHAR(5))
INSERT INTO MyTable (Column1, Column2) VALUES ('A', 'Foo')
BEGIN TRANSACTION
UPDATE MyTable SET Column2 = 'Bar' WHERE Column1 = 'A'
ROLLBACK TRANSACTION
SELECT * FROM MyTable -- OUTPUTS 'A', 'Foo' because the transaction was rolled back.
DROP TABLE MyTable
@DForshner
DForshner / BarsController.cs
Last active December 16, 2015 09:48
Passing a parameter object to a WebAPI controller GET method. Useful when you have a large number of filters to a controller.
/// <summary>
/// Note: Need [FromUri] to tell the model binder to pull the model from the URI and not the body.
/// </summary>
[HttpGet]
public HttpResponseMessage Get([FromUri]BarFilterParameters parameters)
{
// ...
}
public class BarFilterParameters
@DForshner
DForshner / DataTypes.js
Last active December 17, 2015 00:59
Javascript Data Types
// Data type constructor function. JavaScript does not have classes.
function Pet(config) {
this.name = config.name;
this.species = config.species;
}
// Define methods on prototype so all pet objects and any data types that inherit from pet have this method.
Pet.prototype.getSpecies = function() {
return this.species;
};
@DForshner
DForshner / PrivateVariables.js
Last active December 17, 2015 00:59
Javascript Private Variables
// Javascript doesn't have private variables but closures can be used to hide data.
var myClass = (function() {
var privateInfo
setPrivateInfo = function(privateInfoToSet) {
this.privateInfo = privateInfoToSet;
console.log("Set privateInfo: " + this.privateInfo);
};
@DForshner
DForshner / PrototypePattern.js
Last active December 17, 2015 00:59
JavaScript Prototype Pattern
// The Prototype pattern allows objects to be created
// based on a template of an existing object using cloning.
var Dragon = {
name: "TROGDOR"
};
//Object.create takes its first argument and applies it to the prototype of your new object.
var myDragon = Object.create(Dragon);
@DForshner
DForshner / NaiveFibonacciSequenceGenerator.cs
Last active December 17, 2015 02:08
C# IEnumerable fibonacci sequence
using System;
using System.Collections;
public class NaiveFibonacciSequenceGenerator : IEnumerable
{
private readonly int sequenceSize;
public NaiveFibonacciSequenceGenerator(int sequenceSize)
{
this.sequenceSize = sequenceSize;