Skip to content

Instantly share code, notes, and snippets.

View theorigin's full-sized avatar

Andy Robinson theorigin

View GitHub Profile
@theorigin
theorigin / NamingConventions
Created July 30, 2013 10:17
SQL Server naming conventions
-- Tables
-- Basically the table name (singular)
CREATE TABLE [dbo].[Product] (...)
-- Columns
-- Identity
[Id] INT NOT NULL IDENTITY (1, 1) -- Id column with auto increment
@theorigin
theorigin / CsvParse.vb
Created July 30, 2013 15:08
Parse CSV using Microsoft.VisualBasic.FileIO.TextFieldParser
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
@theorigin
theorigin / UDT.sql
Created September 27, 2013 09:39
SQL table types - example of passing them to an SP
declare @p2 dbo.Dictionary
insert into @p2 values(N'orderReference',N'654')
insert into @p2 values(N'earliestRequestedDeliveryDate',N'2013-09-02')
insert into @p2 values(N'transactionId',N'123')
declare @p3 dbo.IndexedDictionary
insert into @p3 values(1,N'productCode',N'59510')
insert into @p3 values(1,N'orderedQty',N'1')
insert into @p3 values(2,N'productCode',N'62754')
insert into @p3 values(2,N'orderedQty',N'3')
@theorigin
theorigin / DatasetToJson.cs
Created October 3, 2013 16:36
Converts a dataset (containing many datatables) into a List<Dictionary<string, object>> which can be converted into JSON and returned from an API. Stored procedure returns the required data in a known format which is then read in C# and push out in JSON. Should be fairly generic i.e. what ever you return from the database will be converted and s…
private IEnumerable<Dictionary<string, object>> DataSetToOrder(DataSet orderDetails)
{
var s = orderDetails.Tables[0].Rows[0][0].ToString();
var tableDefinitions = s.Replace(", ", ",").Split(',');
var tables = tableDefinitions.Select(tableDefinition => tableDefinition.Split('/')).Select(temp => new Tuple<int, int, string>(int.Parse(temp[0]), int.Parse(temp[1]), temp[2])).ToList();
var parent = tables.First(t => t.Item1.Equals(1));
@theorigin
theorigin / IDirectory.cs
Created October 29, 2013 13:41
An interface definition for the System.IO.Directory class. Allows mocking of code that uses any of these methods
namespace VS.Library.Interfaces
{
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;
public interface IDirectory
{
bool Exists(string path);
@theorigin
theorigin / IFile.cs
Created October 29, 2013 13:41
An interface definition for the System.IO.File class. Allows mocking of code that uses any of these methods
namespace namespace VS.Library.Interfaces
{
public interface IFile
{
void Move(string sourceFileName, string destFileName);
bool Exists(string fileName);
void Delete(string fileName);
}
@theorigin
theorigin / SPTemplate.sql
Created January 10, 2014 17:11
TSQL stored procedure template
CREATE PROCEDURE [dbo].[xxx]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @transactionName VARCHAR(32) = REPLACE((CAST(NEWID() AS VARCHAR(36))),'-','')
BEGIN TRY
DECLARE @TranCounter INT;
SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM
sys.tables t
INNER JOIN
@theorigin
theorigin / UrlHelperExtensions.cs
Last active August 29, 2015 13:57
Add a datetime suffix to style/script tags in HTML to force a refresh when they change
public static class UrlHelperExtensions
{
public static string ContentWithHash(this UrlHelper urlHelper, string contentPath)
{
var virtualFile = urlHelper.Content(contentPath);
var actualFile = urlHelper.RequestContext.HttpContext.Server.MapPath(contentPath);
var creationDateTime = (File.Exists(actualFile) ? File.GetLastWriteTimeUtc(actualFile) : new DateTime()).ToString("yyyyddMMHHmmss");
@theorigin
theorigin / JSON-Date-Validate.js
Created August 19, 2014 15:40
Validation of date in JSON using JSON schema
{
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"properties": {
"orderDate": {
"type": "string",
"format": "date"
}
}