21 September 2013

Commit and Rollback Commands in SQL Server



Introduction

            In this article I described Commit and Rollback commands in SQL Server. Rollback and Commit are transaction statements.
A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.
Commit:

            Commit is used for the permanent changes. When we use Commit in any query then the change made by that query will be permanent and visible. We can't Rollback after the Commit.

Syntax :

            begin tran transactionName
                     Command for operation
            commit tran transactionName

            Here transactionName is the name of the transaction and the command for operation is the SQL statement that is used for the operation like making a change or inserting data etc.

Example :
Create table
create table tbl_student(Id int identity(100,1) primary key,Name varchar(200),Age int,Email Varchar(200),Address nvarchar(max))
Commit Example:
begin tran tran_student
insert into tbl_student(Name,Email,Address)values('adi','abc@gmail.com','bangalore')
commit tran tran_student

--tran_student is Transaction Name
Output
Select Commend
select * from tbl_student
Output

Rollback :

            Rollback is used to undo the changes made by any command but only before a commit is done. We can't Rollback data which has been committed in the database with the help of the commit keyword.

Syntax:

            begin tran  transactionName
                 Command for operation
            Rollback tran transactionName

            Here transactionName is the name of the transaction and the command for the operation is the SQL statement that is used for performing operations like to make any change or insert data etc.

Example:

            We want that, if data entered by user has an empId less than 10 then the command is rolled back and a message is shown to the user "An id less than 10 is not valid; query is rolled back".
 
begin tran tran_student
declare @age int;
set @age=12
insert into tbl_student(Name,Age,Email,Address)values('adi',@age,'abc@gmail.com','bangalore')
if(@age<15)
begin
print'An age less than 15 is not valid; query is rolled back';
rollback tran tran_student;
end
else
begin
print 'data is inserted'
end

            Here tran_student is the name of transactions. When we provide age less than 15 then we get

Output:
When we provide age 22 which is greater then 15 then:

begin tran tran_student
declare @age int;
set @age=22
insert into tbl_student(Name,Age,Email,Address)values('adi',@age,'abc@gmail.com','bangalore')

if(@age<15)
begin
print'An age less than 15 is not valid; query is rolled back';
rollback tran tran_student;
end
else
begin
print 'data is inserted'
end

Output:

19 September 2013

Polymorphism in C# & Types of Polymorphism with Example in C#


Introduction : In this article I will explain polymorphism, compile time polymorphism and runtime polymorphism in c#.net with example.
Polymorphism
Polymorphism is one of the principle of object oriented programming. "Poly" means many and "morph" means forms hence the name polymorphism. Polymorphism also refered to as one name many forms or having one name with multiple functionality.
In simple words you can use same method name with different signature or same signature but in different class.So depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.

Types of Polymorphism


·         Compile time polymorphism (or) Static Polymorphism (or) Early Binding (or) Overloading (or) static binding
·         Runtime polymorphism (or)  Dynamic Polymorphism (or) Late Binding (or) Overriding (or) dynamic binding

Compile time polymorphism
Compile time polymorphism  is also called as Static Polymorphism. In Compile time polymorphism  methods are overloaded  with same name but having different signatures.So it is called as method overloading.
Runtime polymorphism
Runtime polymorphism is also called as Dynamic polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.In Dynamic polymorphism methods are overridden so it also called as methodoverloading.

          During run time, Method overriding can be achieved by using inheritance principle and using "virtual" and "override" keyword. so this type of polymorphism can also be called as late binding.In late binding compiler doesn't know what kind of methods it has to call and which can be achieved only during the run time.so it is called as run time polymorphism.

Compile time polymorphism example

public class Class1
{
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
}

            In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding.
Runtime polymorphism example
//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DClass objDc = new DClass();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}
If we run above code we will get output like as shown below
Output
Derived Class
Derived Class

(or)

Example of Compile Time Polymorphism

Method Overloading

- Method with same name but with different arguments is called method overloading.
- Method Overloading forms compile-time polymorphism.

Example of Method Overloading:
class A1
{
void hello()
{ Console.WriteLine(“Hello”); }

void hello(string s)
{ Console.WriteLine(“Hello {0}”,s); }
}

Example of Run Time Polymorphism

Method Overriding

- Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
- Method overriding forms Run-time polymorphism.
- Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
 Example of Method Overriding:

Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}

Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}

static void main()
{
parent objParent = new child();
objParent.hello();
}

Output

Hello from Child.