Pages

Monday, 4 June 2012

Assemblies in .Net

Assemblies can be thought of as the building blocks of your applications.

Without an associated assembly, code will not be able to compile from IL.
As in the Windows DNA world where DLLs and EXEs are the building blocks of applications,

in the .NET world, it is the assembly that is the used as the foundation of applications.
With .NET, it is now possible to have multiple versions of components, or assemblies, running

on the same server side by side. An application will always look for the assembly that built it.
When an application is started in .NET, the application will look for an assembly in the

installation folder. Assemblies that are stored in a local installation folder are referred to as
private assemblies. If the application cannot find the assembly within the installation folder,
the application will turn to the Global Assembly Cache (GAC) for the assembly.

The structure of an assembly

Assemblies contain code that is executed by the Common Language Runtime. The great thing
about assemblies is that they are self-describing.





Assemblies are made up of the following parts:

  • The assembly manifest
  • Type metadata
  • Microsoft Intermediate Language (MSIL) code


The assembly manifest is where the details of the assembly are stored. The assembly is stored
within the DLL or EXE itself. Assemblies can either be single or multifile assemblies and,
therefore, assembly manifests can either be stored in the assembly or as a separate file. The
assembly manifest also stores the version number of the assembly to ensure that the
application always uses the correct version. When you are going to have multiple versions of
an assembly on the same machine, it is important to label them carefully so that the CLR
knows which one to use.



Tuesday, 29 May 2012

Counting null and non-null values in SQL Server


Counting null and non-null values in a single query

--create a table
create table employee(
emp_name varchar(50),
salary int)

--insert data in table
insert into employee values ('Rohit',10000)
insert into employee values ('Rahul',NULL)
insert into employee values ('Ravi',NULL)

--To see the data
select * from employee

--count null and not null value
SELECT count(*) FROM employee WHERE salary IS NULL
UNION ALL
SELECT count(*) FROM employee WHERE salary IS NOT NULL 

************************************************************      
                                            or
************************************************************

SELECT COUNT(*), 'null_tally' AS narrative 
  FROM employee 
 WHERE salary IS NULL 
UNION
SELECT COUNT(*), 'not_null_tally' AS narrative 
  FROM employee 
 WHERE salary IS NOT NULL;

How To Delete a null record in SQL Server


How To Delete a null record


--create a table
create table employee(
emp_name varchar(50),
salary int)

--insert data in table
insert into employee values ('Rohit',10000)
insert into employee values ('Rahul',NULL)
insert into employee values ('Ravi',NULL)

--To see the data
select * from employee

--delete all null record
delete employee where salary=null
--result after above code - (0 row(s) affected)

*******************************************************

--for delete null record we use 'is'
delete employee where salary is null
--result after above code - (2 row(s) affected)

Self-join in SQL Server 2005-08

Self-join in SQL Server 2005-08

A table can be joined to itself in a self-join. Use a self-join when you want to create a result set that joins records in a table with other records in the same table. To list a table two times in the same query, you must provide a table alias for at least one of instance of the table name. This table alias helps the query processor determine whether columns should present data from the right or left version of the table.



For self join in sql you can try the following example:
Create table employees:
emp_idemp_nameemp_manager_id
1JohnNull
2Tom1
3Smith1
4Albert2
5David2
6Murphy5
7Petra5


Now to get the names of managers from the above single table you can use sub queries or simply the self join.
Self Join SQL Query to get the names of manager and employees:
select e1.emp_name 'manager',e2.emp_name 'employee'
from employees e1 join employees e2
on e1.emp_id=e2.emp_manager_id

Result:
manageremployee
JohnTom
JohnSmith
TomAlbert
TomDavid
DavidMurphy
DavidPetra

Understanding the Self Join Example
In the above self join query, employees table is joined with itself using table aliases e1 and e2. This creates the two views of a single table.

from employees e1 join employees e2
on e1.emp_id=e2.emp_manager_id

Here e.emp_manager_id passes the manager id from the 2nd view to the first aliased e1 table to get the names of managers.

Thursday, 10 May 2012

Singleton class in C#



Singleton in C#



The Singleton pattern ensures that a class only has one instance and provides a global point of access to it from a well-known access point. The class implemented using this pattern is responsible for keeping track of its sole instance rather than relying on global variables to single instances of objects.


Examples :


using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}