Posts

Showing posts from 2014

Executing synchronous methods asynchronously

We can achieve certain advantages by writing asynchronous methods. It's pretty simple to write them in C# code: we just need to use  async  and  await keywords properly. Grammar of these keywords can be found in detail in MSDN . In this post I will try to focus on writing few code to invoke a regular synchronous method asynchronously. Let's assume a scenario where we need to perform an expensive database operation when a particular information is changed from the front end. For example: I have an application to manage employee information and I've changed the basic salary for a particular grade using my application. Now I need to update salary of all the employees under that grade. The later operation will be slower one and I don't want this to block me from doing other stuffs. Here is a piece of synchronous code to update salary information: public   bool   UpdateEmployeeSalary ( string   grade ) {      try  ...

Different approaches for paging SQL data

Image
Pagination is a common technique to improve user experience while pulling large chunk of data from SQL. But even pulling paged data could become an overhead while  retrieving  data across multiple tables. Here I will go through some of the techniques for pagination. For illustration I am assuming there is an "Activities" table that holds logs for all activities throughout an application. I will be retriving paged data from this table. Approach 1: using ROW_NUMBER This is the most common approach for getting paged data from SQL query and it works pretty well for simple queries. Here is an example: DECLARE   @StartIndex   INT ,   @EndIndex   INT ; SET   @StartIndex   =   51 SET   @EndIndex   =   100   SELECT   *   FROM   (   SELECT   Count ( 1 )   OVER  ()   AS   TotalCount ,          ROW_NUMBER ()   OVER ...