Logo
14 May 2012 | 1 min. (161 words)

Strongly typed Stored Procedure names in C#

Calling SQL Stored Procedures in ASP.NET as simple text strings can cause problems such as:

Typo in Stored Procedure name. This isn’t a huge issue, but it can be costly if you later need to rebuild the application because of a simple typo! Finding all references to a Stored Procedure. As the stored procedure is stored as a string, the only way you can find all occurrences of the stored procedure is by doing a “Find In Files”. This works fine unless you’re searching for “spFoo” and “spFooBar” also exists.

SqlCommand cmd = new SqlCommand("sp_Foo", conn);

An alternative way would be to store the stored procedure names in a class with a value the same as its name. For example:

public static class StoredProcedureNames {      
   public static string sp_Foo = "sp_Foo";    
}

Then the stored procedure can be called like:

SqlCommand cmd = new SqlCommand(StoredProcedureNames.sp_Foo, conn);

Therefore the stored procedure is now strongly typed, and can be picked up with intellisense.

c-sharp sql-server stored-procedure strong-type
Twitter Facebook

Using jQuery callback functions

…

Related Links

  • LinkedIn
  • Twitter: @curtcode
  • Stack Overflow
  • GitHub: @curtiscde

Stack Exchange

profile for Curtis on Stack Exchange, a network of free, community-driven Q&A sites
Follow @curtiscde

Recent Posts

  • My terminal setup and commands I use on a daily basis when working with GitHub
  • Displaying latest posts on your GitHub profile
  • Using Codecov within a monorepo
  • Displaying Strava stats using webhooks & GitHub Actions
  • Filtering duplicate data points on Chart.js
Theme Bleak by zutrinken Published with Hugo
Menu