Thursday, 27 February 2014

Wednesday, 26 February 2014

Get type information of any object using GetType( )

object obj = new object();
obj.GetType();

string s;
s.GetType();

List<string> StringList = new List<string>();
StringList.GetType();


Tuesday, 25 February 2014

Concatenate Dictionary objects in c#

Dictionary<string, object> d1 = new Dictionary<string, object>();
d1.Add("Name","Talha");
Dictionary<string, object> d2 = new Dictionary<string, object>();
d2.Add("City","Lahore");

// --- Now Concatenate Both Dictionaries using concat Extension method
d1 = d1.Concat(d2).ToDictionary(x=> x.Key , x=> x.Value);

Note:  Here "Concat" is an Extension method  so You have to include it’s Namespace reference on top.

i.e.
using System.Linq;

Friday, 21 February 2014

Dynamic Query in Sql Server

It is used commonly when you are Selecting the Result set on the base of some parameter or condition ,  or to optimize the Sql to generate the accurate query at Runtime according to the Requirements. .

suppose we have a Stored Procedure which takes a single Parameters , which is a table name and generates the query according to it.

Here is a simple Example below:

Create procedure DynamicQuery
(
@tableName varchar(33)
)
as

BEGIN

Declare @SelectQuery Varchar(25)

If (@tableName = 'Teacher')
set @SelectQuery = 'select * from Teacher'
exec (@SelectQuery)

If (@tableName = 'Student')
set @SelectQuery = 'select * from Student'
exec (@SelectQuery)

END

Friday, 14 February 2014

Boxing and Unboxing in c#

Boxing:

int i = 123;
object o = i;  // boxing interger i into object o.

Unboxing:

object o = 123;
i = (int)o;  // unboxing int value from object o to int i.


Select Date Part From DateTime in Sql Server

SELECT CONVERT(VARCHAR(10),GETDATE(),111)


Thursday, 6 February 2014

SQL LIKE Operator

LIKE operator is used to filter Records for a specified pattern in a column.

Syntax:

SELECT column_name(s)
FROM table_name                  

WHERE column_name LIKE pattern;


Use:


SELECT * FROM Persons

WHERE Name LIKE 's%';

Above query will display All those Person whose name Starts with 's'.


Sunday, 2 February 2014

Space Function in Sql Server

Returns a string of repeated spaces

Syntax:

Select FirstName + Space(1) + LastName from Person

IF FirstName is Talha and LastName is Tanweer than Result would be:

Talha Tanweer