Skip to content

Instantly share code, notes, and snippets.

// A C# port of MRZ parser at https://github.com/DoubangoTelecom/ultimateMRZ-SDK/blob/71e507ffc33fb5a24a2fa127c72a41f3755f31e0/samples/c%2B%2B/mrz_parser.h#L1
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
namespace ImranB.Parsers
{
public class MrzData
@imranbaloch
imranbaloch / Mapper.cs
Created August 6, 2015 17:41
Simple Mapper
public static class Mapper
{
public static T2 Map<T1, T2>(T1 t1)
{
var json = JsonConvert.SerializeObject(t1);
return JsonConvert.DeserializeObject<T2>(json);
}
}
@imranbaloch
imranbaloch / gist:6377f69e95aea7c3701f
Created June 3, 2015 02:12
Sort query string and remove the empty ones
private static string SanitizeUrl(string url)
{
var uri = new Uri(url);
var path = uri.GetLeftPart(UriPartial.Path);
path += path.EndsWith("/") ? "" : "/";
var query = uri.ParseQueryString();
var dict = new SortedDictionary<string, string>(query.AllKeys
.Where(k => !string.IsNullOrWhiteSpace(query[k]))
.ToDictionary(k => k, k => query[k]));
return (path + ToQueryString(dict)).ToLower();
//await connection.OpenWithRetryAsync(retryPolicy).ConfigureAwait(false);
//var reader = await command.ExecuteReaderWithRetryAsync(retryPolicy).ConfigureAwait(false);
public static Task OpenWithRetryAsync(this SqlConnection connection,
RetryPolicy retryPolicy)
{
return retryPolicy.ExecuteAsync(connection.OpenAsync);
}
BEGIN TRANSACTION [Tran1]
BEGIN TRY
EXEC('RAISERROR ( ''Create Table Currencies'', 10,1) WITH NOWAIT');
EXEC('
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[Currencies]'') AND type in (N''U''))
BEGIN
CREATE TABLE [dbo].[Currencies]
(
[Id] [INT] IDENTITY(1,1) NOT NULL PRIMARY KEY,
@imranbaloch
imranbaloch / gist:2e1e8158878f845828c6
Last active August 29, 2015 14:13
SqlCommandWrapper
private async Task<IList<Product>> GetProductsAsync()
{
var sqlCommandWrapper = new SqlCommandWrapper("YourConnString", 90);// connection string and timeout
var parameters = new SqlParameter[] {};
return (await sqlCommandWrapper.ExecuteReaderAsync(CommandType.Text, // For stored-procedured no need to pass CommandType param
"Select Top 2 Id, Name From Products",
r =>
new Product
{
Id = (int)r["Id"],
@imranbaloch
imranbaloch / gist:10895917
Created April 16, 2014 15:40
SqlHelper.cs class with async support
// ===============================================================================
// Microsoft Data Access Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
//
// cs
//
// This file contains the implementations of the SqlHelper and SqlHelperParameterCache
// classes.
//
// For more information see the Data Access Application Block Implementation Overview.