Friday, October 31, 2008

Types of Join in SQL Server

Join
By using joins, we can retrieve data from two or more tables based on logical relationships between the tables. Joins indicate how SQL Server should use data from one table to select the rows in another table. Sql server joins are used to combine result data from two or more tables.
There are three types of sql server joins.

1. Inner Join
2. Outer Join
3. Cross Joins

1. Inner Join

Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. Inner joins return rows only when there is at least one row from both tables that matches the join condition. Eg:Retrieving all rows where the student identification number is the same in both the students and courses tables.

2. Outer join
Inner joins eliminate the rows that do not match with a row from the other table. Outer joins, however, return all rows from at least one of the tables or views mentioned in the FROM clause.

There are there 3 types of outer joins.

a. Left outer join
All rows are retrieved from the left table referenced with a left outer join. If matching records are found then it will display data of right table with left table data otherwise put a null values instead of right table data.

b.Right outer join

All rows are retrieved from the right table referenced in a right outer join. If matching records are found then it will display data of left table with right table data otherwise put a null value instead of left table data.

c.Full outer join
All rows from both tables are returned in a full outer join.This joins are combination of both left outer join and right outer join.

3. Cross join

A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table.

Self-join

A table can be joined to itself in a self-join.

Wednesday, October 29, 2008

Dispose method

Dispose method of IDisposable interface performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. This method is used to explicitly release unmanaged resources in conjunction with the garbage collector.

When implementing this method, ensure that all held resources are freed by propagating the call through the containment hierarchy. For example, if an object A allocates an object B, and object B allocates an object C, then A's Dispose implementation must call Dispose on B, which must in turn call Dispose on C. An object must also call the Dispose method of its base class if the base class implements IDisposable.
Because the Dispose method must be called explicitly, objects that implement IDisposable must also implement a finalizer to handle freeing resources when Dispose is not called. Once the Dispose method has been called, it is unnecessary for the garbage collector to call the disposed object's finalizer. Dispose implementation calls the GC.SuppressFinalize method, to prevent automatic finalization.


 
// Implement IDisposable. Do not make this method virtual. A derived class should not be able to override this method.

public void Dispose()
{
// This object will be cleaned up by the Dispose method. Therefore, you should call GC.SupressFinalize to take this object off the finalization queue and prevent finalization code for this object from executing a second time.
GC.SuppressFinalize(this);
}

Finalize method

Finalize method allows an Object to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. This method acts as a safeguard to clean up resources in the event that the Dispose method is not called. Implement a Finalize method to clean up only unmanaged resources such as file handles or database connections. No need to implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.

The scope of the Object.Finalize method is protected. Every implementation of Finalize in a derived type must call its base type's implementation of Finalize. Finalize method is automatically called after an object becomes inaccessible, unless the object is re-registered using ReRegisterForFinalize and exempted from finalization by a call to SuppressFinalize.

Limitations:
1. The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose
method.

2. The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.

3. The thread on which the finalizer is run is unspecified.
Destructors are the C# mechanism for performing cleanup operations.
In C#
// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~MyResource()
{
     Dispose(false);
}
In VB.Net
' This finalizer will run only if the Dispose method does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this class.
Protected Overrides Sub Finalize()
Dispose(False)
      MyBase.Finalize()
End Sub

Tuesday, October 28, 2008

Garbage Collection

The garbage collector manages the allocation and release of memory for the application. Each time the new operator is used to create an object, the runtime allocates memory for the object from the managed heap. As long as address space is available, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.