Saturday, September 16, 2017

Func, Action And Local Function in C# 7.0

Func and Action generic delegates and Local function in C# 7.0. The Func and Action are already available in C# while Local function is introduced in C# 7.0.

#FuncActionLocal Function
1It is a generic delegate.It is a generic delegate.It is method which defined in another method body.
2It has 0 to 16 parameters.It has 0 to 16 parameters.It has 0 to 32 parameters but it’s code smell with too many parameters.
3It always returns a value.It doesn’t return value.It returns value that could be void.
4It must be defined before its call.It must be defined before its call.It could be defined later in the method.

Func

using static System.Console; 

using System; 
   
namespace PrimeNumberApp 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            Func<int, bool> checkPrimeNumber = x => 
            { 
                bool isPrime = false; 
                int i; 
                for (i = 2; i <= x - 1; i++) 
                { 
                    if (x % i == 0) 
                    { 
                        isPrime = false; 
                        break; 
                    } 
                } 
                if (i == x) 
                { 
                    isPrime = true; 
                } 
                return isPrime; 
            }; 
   
            WriteLine("Enter a number"); 
            int number = Convert.ToInt32(ReadLine()); 
   
            bool isPrimeNumber = checkPrimeNumber(number); 
            WriteLine($"{number } is {(isPrimeNumber ? "" : "not")} prime number"); 
            ReadKey();             
        } 
    } 
}


Action


using System; 

using static System.Console; 
   
namespace FibonacciSeriesApp 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            Action<int> GetFibonacciSeries = x => 
             { 
                 int starter = 0, next = 1, number = 0; 
                 Write($"{starter} {next}"); 
                 for (int i = 2; i < x; i++) 
                 { 
                     number = starter + next; 
                     Write($" {number}"); 
                     starter = next; 
                     next = number; 
                 } 
             }; 
            WriteLine("Enter a length"); 
            int num = Convert.ToInt32(ReadLine()); 
            GetFibonacciSeries(num); 
            ReadKey(); 
        }       
    } 
}

Local Function


using System; 

using static System.Console; 
   
namespace LocalFunctionApp 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            WriteLine("Enter a number to calculate factorial"); 
            int num = Convert.ToInt32(ReadLine()); 
            long fact = GetFactorial(num); 
            WriteLine($"{num} factorial is {fact}"); 
            long GetFactorial(int number) 
            { 
                return number == 0 ? 1 : number * GetFactorial(number - 1); 
            } 
            ReadKey(); 
        } 
    } 
}



Saturday, September 2, 2017

Change stored procedure output column names and data types

SQL Server 2012 introduced “WITH RESULT SETS” option which can be used with EXECUTE command to redefine the metadata of result sets being received from stored procedures. This option can handle single as well as multiple result sets.
WITH RESULT SETS” option can be useful if we want to reuse an existing stored procedure’s result set(s) with different column name and / or data type. 

Using WITH RESULT SETS to redefine column names and data types of stored procedure’s result set

Using WITH RESULT SETS option, we can redefine the metadata of result set(s) of a stored procedure during execution. We can use the below code to change the column names and data types of result sets(s) of stored procedure without making any change in the existing code:

EXEC [dbo].[uspGetEmployeeManagers] @BusinessEntityID = 101
WITH RESULT SETS
(
 (
 ReportingLevel INT,
 BusinessEntityID INT,
 FirstName NVARCHAR(50),
 LastName NVARCHAR(50),
 OrganizationNode NVARCHAR(MAX),
 ManagerFirstName NVARCHAR(50),
 ManagerLastName NVARCHAR(50)
 )
)


Below is the syntax to define column names and data types in case we need to handle multiple result sets:

EXEC ProcedureName @Param = 'Value'
WITH RESULT SETS
(
--Result set 1
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
),
--Result set 2
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
),
--Result set 3
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
)
--And so on
)

WITH RESULT SETS option – Limitations

  1. We cannot redefine a subset of columns – Either all columns of a result set needs to be defined or none of them can be defined using WITH RESULT SETS option.
  2. Cannot redefine a subset of result sets – In case the stored procedure return multiple result sets, either all result sets (with all columns) need to be defined or none of them can be defined.
  3. Cannot change the order of the columns and result sets – We cannot change the sequence of columns and result sets.
  4. Cannot use calculation or type casting – We cannot use calculations or type casting with the columns.

Encrypt/Decrypt the App.Config

Program.cs using System; using System.Diagnostics; using System.IO; namespace EncryptAppConfig {     internal class Program     {         pr...