Monday, 27 June 2016

Basics Of C#.net

Advantage of .NET
 -      -> Platform independence
     ->  Security
     ->  Automatic memory management

    Development of .net framework
      The development of .net framework started the Microsoft in late 90’s under the name NGWS(Next Generation Windows services/System).To develop the framework, first a set of specification have been prepared known as CLI(Common language infrastructure) specification. CLI specification are open specification standardized under ISO & ECMA(Europium computer Manufacture Association) that give a chance for develop the framework by anyone. The CLI specification talks about these important things:-
       ->  CLS(Common Language Specification)
          ->  CTS(Common Type System)
       ->  BCL(Base Class Library)
       ->  VES(Virtual Execution System)
       ->  CLR(Common Language Run-time)

CLS (Common Language Specification)
            CLS is set of basic rules of all the .Net Language. It has to adopt to inter-operate with each other, Most important after compilation of any .net language program they should give the same output code i.e. CIL code.
As for example:-

  C#.net ->  CSC  ->  CIL Code {C Sharp Compiler (CSC)}
 VB.net ->  VBC ->  CIL Code {Visual basic Compiler (VBC)}
 J#.net   ->  JSC  ->  CIL Code  {J sharp Compiler (JSC)}
 F#.net  ->  FSC  ->  CIL Code  {F Sharp Compiler (FSC)} 

CTS (Common Type System)
               According to this all languages of .Net has to adopt uniform data structure i.e. similar data types must be same in size under all language of .net, so that if any two language wants to communicate with each other size problem with data type will not come.

NOTE:
----------
CLS & CTS are foundation for language inter-operability of .net languages.

Que. Data types are mismatched. How to inter-operability work?
Ans. Before compilation re-usability is not possible. It is only                 possible after compilation. Because after compilation there            will be no error. Any data types that is used in any .Net language       once after compilation get converted into IL types as following:-

          C#.Net Data type      ->      Compiled   ->       CIL Code
         ----------------------                                        ------------------

                    Int                                                    int32
                    Float                                                single               
                    bool                                                  Boolean

          VB.Net Data type     ->      Compiled   ->    CIL Code
          ----------------------                                     ------------------

                    Integer                                             int32
                    single                                               single
                    boolean                                            boolean
So the data types name will be different from language to language, but even if names are different, similar data types will be uniform in size.

So, in IL (Intermediate language) all data type will be same. When we want to consume the code of a language from other .net language. The data type of 1st language are first converted to IL type and then presented to the 2nd language as its understandable data types as following:-




Library

-> Library is nothing but it is a set of re-usability functionality.
-> In the C/C++ library are called header files.
-> In java library are called Packages
-> And in .Net are called Base class library.

BCL (Base Class Library)
                  A library is a set of reusable functionalities where each and every programming language has built in library. Same as .Net languages are provided with built in libraries known as Base class libraries. The specialty of these library is that they can be consumed under any .net language.

                  



NOTE:-
-----------
BCL are best example of language inter-operability because these library are developed in c# language & can be consumed from any .net language.

.NET Framework Versions:-
       


RTM- Release to manufacturer 



.Net Framework Architecture:-

                       
                   fig: .Net Framework Architecture

      TPL - Task Parallel Library
         PLINQ - Parallel Language Integrated Query
      LINQ - Language Integrated Query
     WPF - Windows Presentation Foundation
     WCF - Windows Communication Foundation
     WF/WWF - Windows Workflow Foundation
     ADO - Active-x Data Objects
     ASP - Active Server page

CLR or VES
It is execution engine of .NET Framework, where all .Net applications are running under supervision of CLR & it provides various features (benefits) to applications like :-
       ->Security
       ->Platform Independent Management
       ->Automatic Memory management
          ->Runtime Error Handling


CLR internally contains the following things in it :-
        1. Security Manager
     2. Class loader
     3. JIT (Just In Time) Compiler
     4. Garbage Collector
     5. Exception Manager

=> Security Manager is responsible for taking care of the security of the application i.e. it will allow application to interact directly with the OS or OS to interact with application.








Read More »

Monday, 6 June 2016

CURSOR

      What is Cursor ? 
                  It is a memory allocation. i.e. used for storing multi row select statements data under the program. Normal variables can store only one value at a point of time where as a cursor can store n number of records at a point of time. 
To work with a cursor we need to understand cursor management which involves in 5 steps.

   Step 1 : Declaring a Cursor
   Step 2 : Opening a Cursor
   Step 3 : Fetching data from the Cursor
   Step 4 : De-allocation the Cursor
   Step 5 : Closing a Cursor 
  
  Declaring a Cursor 
              In this process we declare a cursor and intimate to the database engine that we want a memory  allocation for processing the select statement associate with the cursor declaration. 
 Syntax:-
                Declare Cursor_name cursor
                [ Local | Global ]
                [ Forward only | Scroll ]  
                [ Static | Dynamic ]
                for <Select Statement>

  Opening a Cursor 
                In this process it will allocate the memory for the cursor and loads that a into the cursor by execution the select statement  specified on the declare cursor.
  Syntax:-  
               open <cursor_name> 
Note:
    After loading the data into the cursor it will also provide a pointer for accessing the data from the cursor.

Fetching data from the cursor 
                In this process we retrieve one by one record from the cursor for processing.
  Syntax:-
                Fetch First | Last |Next | Prior | Absolute n | Relative n                       from cursor_name into  <variables>

When we perform  a fetch operation it will return status of the last fetch statement and value can be any of the following.

   0The fetch statement was successful.
 - 1 : The fetch statement failed or the row was beyond the result set
 - 2 : The row fetched is missing.

 Closing the cursor  
                In this process it releases that current result set or data from the memory but leaves the data structure  available for re-opening. 
   Syntax:- 
                Close <cursor_name>
   
De-Allocation the Cursor  
                    In this process it will remove the cursor references and De-allocates the data structure of the cursor.
   Syntax:- 
              Deallocate <cursor_name>; 

For Example applying all step :-
----------------------------------------
Suppose we have a table Employee
   Step 1 :   
        Declare @ename varchar(50), @job varchar(40);       
        Declare EmpCur Cursor for select Empname,job from employee
Step 2 :
        open empcur
Step 3:
        fetch next from empcur into @ename,@job
        while @@fetch_status=0
          Begin
                print @ename + ' ' +@ job;
                fetch next from empcur into @ename,@job;
          End
Step 4 :
       close empcur;
Step 5:
       deallocate empcur;
Note:-
     The value that returns by the fetch statement is stored under an implicit local variable  '@@fetch_status'

 [Local or Global ] cursor
---------------------------------
                                 If the cursor is declare as 'local' the scope of the cursor is only to the program in which the cursor is declare. So in the end of the program execution even if we did not De-allocate the cursor also the cursor gets De-allocate implicitly.
Ex:-       
        Declare @empno int
     Declare EmpCur Cursor local for select Empno from employee
        open empcur;
        fetch next from empcur into @empno;
        while @@fetch_status=0
          Begin
                print @empno;
                fetch next from empcur into @empno;
          End
      close empcur;
      deallocate empcur;
                                               Open a new query window and write the above code in it, we declare the above cursor as a local cursor. So even if we did not de-allocate the cursor also in the end of the program the cursor gets de-allocate. So to test this declare cursor statement and execute the program for second time get the error.


Note:-
---------
  If the cursor is declare as 'global' and not de-allocated that cursor can be used in the other program also with in the connection.
Ex:-       
 Declare @empno int
    Declare EmpCur Cursor global for select Empno from employee
        open empcur;
        fetch next from empcur into @empno;
        while @@fetch_status=0
          Begin
                print @empno;
                fetch next from empcur into @empno;
          End
      close empcur;
      deallocate empcur;
write the above program in a new connection window and execute, we declare the cursor as global in a program and did not de-allocate cursor that means  without re-deallocate.
To test this comment the declare cursor statement and execute the same program for n number of times the connection.
Ex:-

Declare @empno int
--declare Empcur cursor global for select empno from Employee
--open empcur
 fetch next from empcur into @empno
  while @@FETCH_STATUS=0
      begin
         print @empno
         fetch next from empcur into @empno
     end

close empcur

Note:-
-----------
All global cursors declared under a connection or de-allocation once we close the connection.

[ Static or dynamic ] cursor


-----------------------------------
                           If a cursor is declared as a static after opening the cursor and loading data into the cursor if any modification are made to the data in the table those changes will not be reflected in to the cursor. But 
If the cursor is declare as dynamic the modification of the table reflects into the cursor even after the cursor is open also.
Ex:-
    Declare @sal money
    declare Empcur cursor Static for select salary from Employee           where Empno=1001
      open empcur
       update employee set salary+=500 where Empno=1001
       fetch next from empcur into @sal
    print @sal
close empcur

  output:
-----------
In the above case we declare the cursor as static and open the cursor for loading data into the cursor. So, After opening the cursor the update operation we performed on the table will not reflect into the cursor. So, Print statement will print the old salary of the employee but not in updated value.
Execute the same program by changing static as dynamic and watch the output the print statement will display the new salary of the employee.
Ex:-
    Declare @sal money
    declare Empcur cursor dynamic for select salary from Employee           where Empno=1001
      open empcur
       update employee set salary+=500 where Empno=1001
       fetch next from empcur into @sal
    print @sal
close empcur
  output:
-----------

[ Forward only or scroll ] cursor


----------------------------------------
                                          In the cursor is declare forward-only we can navigate only to the next record from the current cursor position. The only fetch method support in forward-only cursor is fetch next.
Where as if a cursor is declared as scroll we can navigate to any record under the cursor from any position because scroll cursor support 6 fetch method.
  i) Fetch Next
 ii) Fetch prior
iii) Fetch  First
iv) Fetch  Last
 v) Fetch  Absolute
vi) Fetch relative n

Ex:-
     Declare @empno int
     declare Empcur cursor scroll for select empno from Employee
     open empcur
        fetch last from empcur into @empno
             print @empno
        fetch prior from empcur into @empno
             print @empno
        fetch first from empcur into @empno
             print @empno
        fetch next from empcur into @empno
             print @empno
        fetch absolute 3 from empcur into @empno
             print @empno
        fetch relative -3 from empcur into @empno
             print @empno
  close empcur

Output:-
-----------

Read More »

Thursday, 2 June 2016

T-SQL Programming (PL/SQL)

T-SQL/  PL/Sql:-
      This is the extension of SQL providing High level programming capabilities. SQL does not provide the following :-
 1) Step by step execution the code.
 2) Condition branching and condition looping.
 3) Error handling mechanism.
                                           To overcome these problem SQL was added with procedure process capabilities and introduce as PL/SQL or T-SQL.
T-SQL is all about programming providing :-
  -> Step by step execution the code.
  -> Condition branching and condition looping.
  ->  Structure error handling mechanism through try-catch block.
   
   T-SQL programs are two types
       i) Anonymous block
      ii) Sub-Programs.

        Anonymous block
       -------------------------
                    An Anonymous block is a code block i.e defined at a particular point of time for particular client mechanism.


        Sub-Programs:
       ---------------------
                   A Sub-Program as a named of block of code which can be saved to the server and it can be called from any client mechanism when ever its required. Sub-Programs can be defined in 3 ways:
  1) Stored Procedure 
  2) Stored Function
  3) Trigger.

        Declaring variables in the T-SQL Programs :-
    -------------------------------------------------------------

      Syntax : -
              Declare @<variable_Name> <type> [(Size)][----------n].

     Ex:-
              Declare @x int;
              Declare @a varchar(50), @b varchar(25);
      
Assigning the values in the variable:-          
There are two type of values assign in the variable in T-SQL program.
i)                    We can assign a Static value to a variable 
ii)                   We can also assign a value of table column into a variable.

     Assign Static Values:-
    -----------------------------  
       Syntax:-
                      Set @<variable_Name> = <value>;
       Ex:-
                      Set @x=100;
     Assign Values of a Table Column:-
    --------------------------------------------   
      Syntax:-
                 Select @<variable_name> = <Column_name>,                                @<variable_name2> = <column_name2>--------[n] from                  <table_name> [<clauses>]
      Ex:-
              Select @a=ename, @b=job from emp where empno=1001

 

Printing Values :-
 Syntax:-
         print @<var>|<value>
 Ex:- 
      Declare @x int;
      set @x=100;
      print @x;
ex:-
    print 'hello';
Read More »