Executing a Stored Procedure....The Proper Way

As we develop in Microsoft's Entity Framework family, we will at times run into a situation, or by design, the need to execute a Stored Procedure from the database (most likely Microsoft SQL Server).  As we have discovered, they (Microsoft) evidently do not want us to do this inherently.  They hid the direct SProc execution well down into the Database object (off the DbContext object) and even then it has a few names: SqlQuery and ExecuteSqlCommand.  Of course, both of these have their equivalent ASync execution versions.

To make matters even more fun and interesting, is the problem of Ordinal execution.  Any proper DBA will tell you, if asked, that Ordinal execution is a bad implementation pattern.  And you developers, will simply respond: Well then don't change the order in which the Parameters exist.  Then the DBA responds, and the banter battle begins between a Developer and a DBA.

My simple solution to avoid this whole needless verbal battle and disagreement is a very simple Extension built off the DbContext object being developed.

1:  public static IEnumerable<T> Execute<T>(this DungeonContext ctx, string schema , string sproc , params SqlParameter[] parms ) where T : class {  
2:     if( parms.Count() > 0 ) {  
3:        for( int i = 0 ; i < parms.Count() ; i++ ) {  
4:           if( i >= 1 )  
5:              sproc += ", ";  
6:           sproc += parms[ i ].ParameterName + "=@p" + ( i + 1 ).ToString( "D2" );  
7:           parms[ i ].ParameterName = "@p" + ( i + 1 ).ToString( "D2" );  
8:        }  
9:     }  
10:     return ctx.Database.SqlQuery<T>( "Execute " + schema + "." + sproc , parms );  
11:  }  

This simple extension, allows you to make a SProc call, the way developer like and executes the design the way DBA's want Explicit Parameter value assignment

1:  using(var ctx = new DungeonContext())  
2:  {  
3:       ctx.Execute<Object>("DBO", "Get_SomeText",   
4:            new SqlParameter("@Parameter1", "value"),  
5:            new SqlParameter("@Parameter2", "value2")  
6:  }  

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Acquiring List of controls - Classic JS