Posts

Showing posts from June, 2015

ProgrammerBase

Every programmer should inherit from ProgrammerBase :) 1 2 3 4 abstract class ProgrammerBase : ILearn, ICode, IConquer { ... }

Inserting Results from a Stored Procedure Call to a Table

This is handy, bumped into this and I thought it would be nice to take note and share. What this code illustrate is that you can basically use a stored procedure's result and insert it into a table and do your own query. Now why do we want to do that? Say for example the existing query does something and is being used by different modules and touching it would require us to check/test those modules that has a dependency on the  SP. Applying the sample below allows us to just reuse the existing SP and get the resulting data and do our own filters, grouping etc.. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 DECLARE @ Table TABLE ( SPID INT , Status VARCHAR ( MAX ), LOGIN VARCHAR ( MAX ), HostName VARCHAR ( MAX ), BlkBy VARCHAR ( MAX ), DBName VARCHAR ( MAX ), Command VARCHAR ( MAX ), CPUTime INT , DiskIO INT , LastBatch VARCHAR ( MAX ), ProgramName VARCHAR ( MAX ),...

ExecuteScalar<T>

 I came across this while working on a existing code from an ISV. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40   public class SomeDBWrapper : IDisposable { public string ExecuteExecuteScalar_String () { object value = this .ExecuteExecuteScalar(); if ( value == DBNull.Value) return "" ; else return ( string ) value ; } public int ExecuteExecuteScalar_Int () { object value = this .ExecuteExecuteScalar(); if ( value == DBNull.Value) return 0 ; else { return Convert.ToInt32( value ); } } public Int64 ExecuteExecuteScalar_Int64 () { object value = this .ExecuteExecuteScalar(); if ( value == DBNull.Value) return 0 ; else return (Int64) value ; } ...

Automatically Discover and Assign Parameter with Values to a Stored Procedure Call in C#

I'm sure most programmers have run across a situation wherein they have to pass  a lot of parameters to a stored procedure call in C#. Instead of manually typing each parameter in and assigning a value, why not develop a way to automatically discover the parameter, add it to the parameter list of the command object and assign the corresponding value from an entity or model. Here's my implementation using extension method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 /// <summary> /// Automatically assigns parameters to Command.Parameter Collection from List of parameter names /// </summary> /// <param name="that">Command object being extended</param> /// <param name="model">Expando model</param> /// <param name="conn">sQLConnection used to g...