Posts

C# Yield

When building a method that returns IEnumerable, you can use the yield keyword to return results one at a time, rather than returning a full collection. This is useful if your collection would be very large, or if you want results as fast as you can rather than waiting. View code on GitHub

View Job History in SQL Server

create procedure util . ViewJobHistory ( @ExecutionDate date = null , @JobName sysname = null ) as begin declare @executionDateInt int ; set @executionDateInt = year ( @ExecutionDate ) * 10000 + month ( @ExecutionDate ) * 100 + day ( @ExecutionDate ) ; select JobName , JobSuccess , RunDateFormatted + ' ' + left ( RunTimePadded , 2 ) + ':' + substring ( RunTimePadded , 3 , 2 ) + ':' + right ( RunTimePadded , 2 ) [ RunDateTime ] , DurationSeconds + ( 60 * DurationMinutes ) + ( 60 * 60 * DurationHours ) [ TotalDurationSeconds ] from ( select sj . name [ JobName ] , case sjh . run_status when 1 then 'Success' else 'FAIL' end [ JobSuccess ] , left ( cast ( sjh . run_date as varchar ( 8 )

Shrink image with ImageMagick

@ ECHO OFF SETLOCAL FOR / R %%G IN ( *.jpg ) DO CALL :process "%%G" GOTO :end :process SET _inname = %1 SET _outname = %_inname:~0,-5%_1024.jpg" identify - format %%w %_inname% > width.txt identify - format %%h %_inname% > height.txt set / p width = < width.txt set / p height = < height.txt DEL width.txt DEL height.txt ECHO Processing %_inname% ... if %width% gtr %height% call :landscape %_inname% %_outname% if %height% geq %width% call :portrait %_inname% %_outname%      rem convert %_inname% %_inname% EXIT / B :landscape convert %_inname% - resize 1024x - quality 90 %_outname% EXIT / B :portrait convert %_inname% - resize x1024 - quality 90 %_outname% EXIT / B :end

SQL Server Flatten and Unflatten

Assuming you have a table type named SingleColumnText which contains (obviously) a single column of text, named [TextValue]: create function util . Flatten ( @input util . SingleColumnText readonly , @delimiter nvarchar ( max ) ) returns nvarchar ( max ) as begin declare @result nvarchar ( max ) ; select @result = coalesce ( @result + @delimiter , '' ) + TextValue from @input order by PK ; return @result ; end ; go create function util . Unflatten ( @input nvarchar ( max ) , @delimiter nchar ( 1 ) ) returns table as return ( select row_number ( ) over ( order by n ) - 1 [ Idx ] , substring ( @input , n , charindex ( @delimiter , @input + @delimiter , n ) - n ) [ TextValue ] from util . Numbers where n < = len ( @input ) and substring ( @delimiter + @input , n , 1 ) = @delimiter ) ; go

Add timestamp to photo using ImageMagick

rem dptnt.com/2009/04/how-to-add-date-time-stamp-to-jpeg-photos-using-free-software/ @ ECHO OFF SETLOCAL FOR / R %%G IN ( *.jpg ) DO CALL :process "%%G" GOTO :end :process SET _inname = %1 identify - format %%w %_inname% > dttmpfile set / p width = < dttmpfile Set / a pointsize = %width% / 50 rem echo ZZ >> dttempfile DEL dttmpfile ECHO Processing %_inname% ... convert %_inname% - gravity SouthEast - font Arial - pointsize %pointsize% - fill orange - annotate +%pointsize%+%pointsize% "%%[exif:DateTimeOriginal]" %_inname% EXIT / B :end

Type-safe JSON result in ASP.NET MVC

public abstract class BaseController : Controller { protected internal JsonResult < T > Json < T > ( T data ) { return Json ( data , null /* contentType */ , null /* contentEncoding */ , JsonRequestBehavior . DenyGet ) ; } protected internal JsonResult < T > Json < T > ( T data , string contentType ) { return Json ( data , contentType , null /* contentEncoding */ , JsonRequestBehavior . DenyGet ) ; } protected internal virtual JsonResult < T > Json < T > ( T data , string contentType , Encoding contentEncoding ) { return Json ( data , contentType , contentEncoding , JsonRequestBehavior . DenyGet ) ; } protected internal JsonResult < T > Json < T > ( T data , JsonRequestBehavior behavior ) { return Json ( data , null /* contentType */ , null /* contentEncoding */ , behavior ) ; } protected internal JsonResult &

Replace alert with SweetAlert

SweetAlert is a nice simple solution for displaying alert messages. Here’s the basic way to replace the built-in alert, so you don’t have to use the custom swal function: < link href = "/styles/sweetalert.css" rel = "stylesheet" type = "text/css" > < style type="text/css"> . sweet-alert h2 { font-size : 21 px ; font-weight : 500 ; line-height : 23.1 px ; } </ style > <!-- SweetAlert does not work in IE8 or prior --> <!--[ if gt IE 8 | ! IE ]> <!-->    <script src="/scripts/sweetalert.min.js" type="text/javascript"></script>    <script type="text/javascript">        window.alert = function () {            if (arguments == null) {                swal(" ");            } else if (typeof arguments[0] === "number") {                swal(arguments[0].toString());            } else {                swal(argumen