Saturday 17 November 2012

begin transaction | rollback transaction | how to retrieve deleted data | sql server 2008

begin transaction  | rollback transaction | how to retrieve deleted data | sql server 2008

how to rollback deleted query in sql server .
how to retrieve deleted | updated data in sql server by using rollback and begin transaction .
sometimes we think if i will delete that particular row then where it will effect.
specially when you are working on live project then it will give more impact.
so ,in this article i have shown how to retrieve deleted data with help of transaction and rollback in sql server 2005 | 2008.

SELECT * FROM STUD --it will give table data
id name
1 dev
2 deva
4 deva
5 deva
6 deva
8 "dsouza"
8 "d-souza"
 now i want to delete record where id=4 and after deletion i want to retrieve it .
then , what to do.
just write the query .

begin transaction 

delete from stud where id =4

(1 row(s) affected)

now check whether data deleted or it exists.

SELECT * FROM STUD
1 dev
2 deva
5 deva
6 deva
8 "dsouza"
8 "d-souza"


record is deleted .now i want to back fourth record what i will do , i will just write rollback .
rollback transaction
Command(s) completed successfully.


SELECT * FROM STUD
1 dev
2 deva
4 deva
5 deva
6 deva
8 "dsouza"
8 "d-souza"
here is your  record.but don't forget to use begin transaction  at the time of deletion of record.
else , it Will be difficult  to retrieve data again .

top query in sql server | select top 1*

top query in sql server | select top 1*

How to use and write top query in sql server | My sql | Oracle
top query will be same in every database but  thing is that to know about table name and field of particular table.


select * from stud 

1 dev
2 deva
4 deva
5 deva
6 deva
8 "dsouza"
8 "d-souza"


Now I want to select one row using top .
select top 1* from stud

1 dev


Now I want to select last record using top.
select top 1* from stud order by name
8 "d-souza"

now its time to work something real means if i want to select 5th record in table where
there is  more than 100 or 8 record.
so , we have to use subquery for that and  create one  Instance table


SELECT TOP 1  FROM
(SELECT TOP 5 * FROM stud ORDER BY id ASC) AS LAST
ORDER BY id DESC

6 deva

what i did over here step first ..
i wrote top 5 * query so it will select top 5 records

SELECT TOP 5 * FROM stud ORDER BY id ASC
1 dev
2 deva
4 deva
5 deva
6 deva

i will get output like this.
now after that i want 5th record so i did top 1 * from table by creating  instance of table AS LAST.
using top you can select  5th record from  bottom or top.but you should know about syntax else it will give error .

Tuesday 13 November 2012

Even | Odd number | 1 to 10 ..n | vb.net source code

Even | Odd number | 1 to 10 ..n | vb.net source code


Even number is a number which is divisible by 2 .
Odd number is number which is not divisible by 2.
lets take examples.
4 /2=2.
when we divide number 4 to number 2 it will not gives remainder so four is Even number .
second examples is 5 divided by 2 i.e 5/2 remeainde will be 1.so 5 is odd number.
in this program it shown to create function in vb.net and call that function in any event like
button click event.
   
Public Sub even_number()
        Dim takeval As Integer = txtVal.Text

        Dim first As Integer = 0
        While first < takeval
            If (takeval Mod 2) = 0 Then


                Response.Write(takeval)

            Else
                Response.Write("<br/> ")

            End If
            takeval -= 1
        End While

    End Sub

Swap values of two variables using call by value | output | comments

Swap values of two variables using call by value | output | comments

How to write program to swap two numbers using call by value in c .
source code to swap two number .
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\nValues of a and b within the function: %d %d",a,b);
}
int main()

{
int a,b;
//Accept value of a & b
printf("\nEnter value of a:");
scanf("%d",&a);
printf("\nEnter value of b:");
scanf("%d",&b);
//print values before calling swap function
printf("\nValues of a and b before calling the function: %d %d",a,b);
//Calling swap function
swap(a,b);
//print values after calling the swap function
printf("\nValues of a and b after calling the function: %d %d",a,b);
}

sscanf() function | c program | string

sscanf() function | c program | string


This function is same as scanf() function but it reads from string rather than standard input.
/*Program to illustrate the use of sscanf() function*/
Include header file
int main()
{
//declare two strings
char s1[20]="1234";
char s2[20]="345.67";
//declare int and float variables
int n;
float f;
//convert the variables into string
sscanf(s1,"%d",&n);
sscanf(s2,"%f",&f);
//print the converted strings
printf("Value of n=%d\nValue of f=%f\n",s1,s2);
}

sprintf() function | c program | source code

sprintf() function | c program | source code


c program sprintf() function use in program with source code.

This function is same as printf() function but it sends the formatted output to a string instead of screen.
So we can convert a variable data type into string using sprintf() function.

/*Program to illustrate the use of sprintf() function*/

include header file
int main()
{
//declare two strings
char s1[20];
char s2[20];

//declare int and float variables

int n=7890;

float f=2345.67;
//convert the variables into string
sprintf(s1,"%d",n);
sprintf(s2,"%0.1f",f);
//print the converted strings
printf("s1=%s,s2=%s\n",s1,s2);

}

how to swap two values in c program | call by reference in c code

how to swap two values in c program  | call by reference in c code

swap values of two variables using call by reference comments
How to write C Program to swap values of two variables using call by reference
include header files


//function to swap values
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("\nValues of a and b within the function: %d %d",*a,*b);
}


int main()
{
int a,b;
//Accept value of a & b
printf("\nEnter value of a:");
scanf("%d",&a);
printf("\nEnter value of b:");
scanf("%d",&b);
//print values before calling swap function
printf("\nValues of a and b before calling the function: %d %d",a,b);
//Calling swap function
swap(&a,&b);
//print values after calling the swap function

printf("\nValues of a and b after calling the function: %d %d",a,b);
}

How to write C Program to calculate L.C.M and G.C.D of two numbers using function

How to write C Program to calculate L.C.M and G.C.D of two numbers using function

Program source code
.first of all create two function first function gcd to find out gcd() and next lcm().
we need to pass two parameter  in given function .

void gcd(int,int); // created function for gcd
void lcm(int,int); // created function for lcm
int main()
{
int a,b;
printf("Enter two numbers: \t");
scanf("%d %d",&a,&b);
lcm(a,b);
gcd(a,b);
return 0;
}


//function to calculate l.c.m
LCM=Least Common Multiple or lowest common multiple

How to find  LCM of 3 and 5 by manually  .
Multiples of 3 are 3,6,9,12,15,18 etc..


and the multiples of 5 are 5,10,15,20, etc... 15 is common in both case.
so lcm of 3 and 5 is 15.

void lcm(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m
m=m+a;
else
n=n+b;
}
printf("\nL.C.M of %d & %d is %d",a,b,m);
}

//function to calculate g.c.d
GCD =Greatest common divisor , One number divides two number like
12  and 20 is divisible by 4 ,
hence gcd of 12 and 20 is 4.
void gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
printf("\nG.C.D of %d & %d is %d",a,b,m);

Void Pointer example in c language

Void Pointer example in c language

 How to use of void pointer in c language
include header files
main()
{
int a=10,*j;
void *k;
j=k=&a;
j++;
k++;
printf("\n %u %u ",j,k);
}
Explanation:

Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type.
No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.

array in c program | Array example with Explanation

array in c program  | Array example with Explanation

Array in cprogramming
/*Array in c program with code*/

include header file
main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);

}

Explanation:
As we know array name is the base address for that array. Here s is the base address and i is the index number/displacement from the base address. So, in directing it with * is same as s[i]. i[s] may be surprising.
 But in the case of C it is same as s[i].

Token pasting operator in c language with program source code.

Token pasting operator in c language with program source code.


/* use of token pasting operator with Output */
//Token pasting operator is used to concatenate two tokens into a single token.
with program source code.


#define PASTE(a,b) a##b
#define MARKS(sub) marks_##sub
int main()
{
int k1=15,k2=60;
int marks_os= 95;
int marks_ds=99;

//converting statement below to printf("%d %d",k1,k2)

printf("%d %d ",PASTE(k,1),PASTE(k,2));

//converting statement below to printf("%d %d",marks_os,marks_ds)
printf("%d %d ",MARKS(os),MARKS(ds));
}

Nesting of macros | check alphabet using ISALPHA | Program code

Nesting of macros | check alphabet using ISALPHA | Program code

Nesting of macros in c language with output.
How to write code for nesting of macros in c language.
By using if and else condition we can check whether entered value is alphabet or not.
simply by using ISALPHA Keyword.
ISALPHA will check about the alphabet or not.
 include header ....
 #define ISLOWER(c) (c>=97&&c<=122)
 #define ISUPPER(c) (c>=65&&c<=90)
 #define ISALPHA(c) ISLOWER(c) || ISUPPER(c)
int main()  // starting of main
{
char chr;
printf("Enter a character:\n"); // taking value from user
scanf("%c",&chr);  
if (ISALPHA(chr) ) // cheking condition
{
printf("%c is an alphabetical character.\n",chr);
}
else  // going in else part
{
printf("%c is not an alphabetical character.\n",chr);
}
return 0;
}

Sunday 11 November 2012

perfect number in vb.net from 1 to n | source code

perfect number in vb.net from 1 to n | source code
Number which is sum of its factors  like
factor of 6 is 1*2*3.
so 1+2+3=6
sum of factors is =number itself
hence 6 is perfect number .after that 28 is perfect number.
perfect number from 1 to 1000
is 6, 28, 496
Public Sub perfect()
        Dim takeval As Integer = txtVal.Text
        Dim valstore As Integer = 1
        Dim flag As Integer = 0
        Dim val2 As Integer
        Dim Sum As Integer = 0
        For val2 = 2 To (takeval - 1)
            valstore = 1
            Sum = 0
            While valstore < val2
                If (val2 Mod valstore = 0) Then
                    Sum = Sum + valstore
                End If
                valstore += 1
            End While
            If Sum = val2 Then
              Response.Write(Sum & "is perfect number")

            End If

        Next


    End Sub