Wednesday, April 28, 2010

Fetching Random Rows From SQL Server

Fetching Random Rows From SQL Server
Recently I needed to fetch random rows from a SQL server table. If you have an integer column then using RAND() function goes well. However in my case there was no number column. In such cases you can use the following query:

SELECT TOP
FROM
WHERE
ORDER BY NEWID()The key is the use of NEWID() function that returns a GUID. An example query would look something like this:

SELECT TOP 10 *
FROM Employees
ORDER BY NEWID()This way is also useful for selecting data for testing purposes.

Tuesday, April 27, 2010

Extension Methods

When you have source code of the classes under consideration at your hand then things are easy. You probably just add the extra methods to those classes. What if you don't have source code? Well.
In such cases one approach is to inherit the existing classes and add extra methods to the child classes.
However, this way may not be always correct and possible in terms of your application design and OO principles. Luckily, C# offers a quick way to extend your class functionality through a feature known as Extension Methods.
Extension methods allow you to extend existing types without inheriting them.

Constructors

Same name as of class
be default return type is void

System.object

Every custom type you create or class you create inherits from the base class System.object

It defines the default constructor which is called everytime time u call new operator

it is used to inilize the object

so this means default constructor is called everytime a new opration is called

Generilization and Specialization

This is another name for the Inheritance
Generilization is the general features of the code in the class
and spelization are the special features of the class

Monday, April 26, 2010

Immediate Window

working with the memory directly

start with ? and type the objects and that would be effected.

Code Snippets

There are the readymade code templates used in the VS IDE.

e.g type if TAB +TAB (hit tab twice)

type for TAB+TAB

switch TAB+TAB

one of the commonly usedone is property and for this type
PROPFULL TAB+TAB
and this would create a get set property as well the private variable
private int myVar

public int MyProperty
{
get { return myVar;}
set {muVar=values;}
}
and if you wanna change int to string change string and hit tab and all the references would be effected automatically

In order to find all the snippets you can click CTRL+K+X
also you can find this in EDIT +Insert Snippet from the menu

to creat our own you need to snippet editor avaliable for free

Association

There are two basic types of association
Aggregation & Containment

the best word to express it would be custome reference types

e.g
Class Engine { public int HoursePower; }

class Auto { public Engine Engine; }

Main Class

{
PSVM {
Auto myCar=new Auto();
myCar.Engine=V6;
Console.writeline(myCar.Engine.HoursePower.toString());
}

relation between mycar and Engine is Aggreation

What are Helper methods

Helper method is the common method which contains the common functionality of a group of code that is used in the class. So insted of writing the code againg and again developer write a HELPER method that can be used for recurring functionality.

most common examples for these are

common methods to output
formating things
mutliple use
refracting

What are properties

these are the gate keepers for our private fields.

Camel Case

Camel Case
it starts with the small letter and for every word it is Caps first letter

public string aaaAaa
private int intNumber


Pascal Case
Also everything that is public should always start with Caps letter
and everything that is private should always start with small letter