Skip to content

Instantly share code, notes, and snippets.

using System.Runtime.InteropServices;
using System.Diagnostics;
static public class FileUtil
{
[StructLayout(LayoutKind.Sequential)]
struct RM_UNIQUE_PROCESS
{
public int dwProcessId;
public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
@bitsprint
bitsprint / cdnorfallback.js
Created October 22, 2013 14:14
CDN or fallback
<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-2.0.0.min.js">\x3C/script>')</script>
@bitsprint
bitsprint / angular-trigger-event.js
Created October 11, 2013 14:28
Angular - trigger event on scope from console example
angular.element('#orders').scope().$emit('getorderreport')
@bitsprint
bitsprint / DictionaryExtensions.cs
Created October 7, 2013 19:28
Dictionary Extensions
public static void SmartAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
try
{
dictionary.Add(key, value);
}
catch (ArgumentException e)
{
throw new ArgumentException("The key '{0}' already exists.".ToFormat(key), e);
}
@bitsprint
bitsprint / topparenthierachy.sql
Created September 18, 2013 13:19
Maintain the top parent record in a hierarchy. Here we specify the hierarchy level in the query as we traverse the hierarchy. Ultimately we can select only the records at the highest level giving us the top parent record at every step in the hierarchy.
CREATE TABLE #Test (CompanyID INT, CompanyName VARCHAR(20), LinkedCompanyID INT)
INSERT INTO #Test
SELECT 1, 'Company A', NULL UNION
SELECT 2, 'Company B', 1 UNION
SELECT 3, 'Company C', 2 UNION
SELECT 4, 'Company D', 2 UNION
SELECT 5, 'Company E', 4 UNION
SELECT 6, 'Company F', 3 UNION
SELECT 7, 'Company G', NULL
@bitsprint
bitsprint / Utils.Date.sql
Created September 17, 2013 09:26
Round date to midnight
-- SQL 2008
DECLARE @CurrentDate DATETIME = (cast(GETDATE() as date))
-- Pre SQL 2008
DECLARE @CurrentDate DATETIME = DateAdd(Day, Datediff(Day, 0, GetDate()), 0)
@bitsprint
bitsprint / CsvParser.cs
Created September 16, 2013 13:02
Csv Parser
using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser("C:\\testfile.txt")) {
parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
parser.SetDelimiters(",");
string[] currentRow = null;
while (!parser.EndOfData) {
try {
currentRow = parser.ReadFields();
string currentField = null;
foreach (string field in currentRow) {
currentField = field;
@bitsprint
bitsprint / Types.Dictionary.1.sql
Last active December 20, 2015 23:28
Indexed Dictionary (User Defined Table Type)
/****** Object: UserDefinedTableType [dbo].[IndexedDictionary] ******/
IF EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'IndexedDictionary' AND ss.name = N'dbo')
DROP TYPE [dbo].[IndexedDictionary]
GO
/****** Object: UserDefinedTableType [dbo].[IndexedDictionary] ******/
CREATE TYPE [dbo].[IndexedDictionary] AS TABLE(
[Index] [int] NULL,
[Key] [nvarchar](255) NULL,
[Value] [nvarchar](255) NULL
@bitsprint
bitsprint / Utils.Tables.1.sql
Created August 1, 2013 12:54
Truncate all the tables
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'TRUNCATE TABLE ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
@bitsprint
bitsprint / ControlExtensions.cs
Created June 27, 2013 14:16
Control Extensions
namespace Web.Extensions.Control
{
using System.Collections.Generic;
using System.Web.Mvc;
// Usage: foos.Select(x => new SelectListItemWithAttributes
// {
// Attributes = new Dictionary<string, object>() { { "foootherid", x.SomeOtherId } },
// Text = x.Name,