Posts

Showing posts from April, 2015

SQL Server Query Optimization

SQL Server Query Optimization ,Internal Storage, Indexes Internale Storage  - Data Pages Create table tab1 in msdb create table tab1 (id int,  item varchar(20))  insert into tab1 values(1,'AAA')  insert into tab1 values(1,'BBB')  select id,item from tab1  select * from tab1  DBCC IND ('msdb',tab1,-1);  DBCC TRACEON(3604)  DBCC PAGE('msdb',1,1510,1) The Internal Page Structure is shown below DBCC execution completed. If DBCC printed error messages, contact your system administrator. PAGE: (1:1510) BUFFER: BUF @0x07DA7290 bpage = 0x1228E000                  bhash = 0x00000000                  bpageno = (1:1510) bdbid = 4                           breferences = 0                     bcputicks = 0 bsampleCount = 0                    bUse1 = 4547                        bstat = 0x10b blog = 0x5adb21cc                   bnext = 0x00000000                  PAGE HEADER: Page @0x1228E000 m_pageId = (1:1510)                 m_headerVersion = 1       

Hierarchical Query example

A Simple demonstration of Hierarchical queries in SQL Server with the help of recursion using Common Table Expression Create Table  CREATE TABLE table_relation_1(     Id INT NOT NULL,     Name VARCHAR(100) NOT NULL,     ParentId INT NULL    ) Insert Rows into the Table ALTER PROCEDURE insert_data AS BEGIN     DECLARE     @maxcount int=10,     @count int=1,     @parentId int     WHILE @count<@maxcount     BEGIN     IF @count=1     set @parentId=null;     ELSE     set @parentId=@count-1;         INSERT INTO table_relation_1 VALUES(@count,convert(varchar(20),@count)+'_USER',@parentId);     SET @count=@count+1;     END END Run a recursive query with the help of CTE to get the Parent Child relationship  with generation. ;with CTE(ChildId, ChildName,generation,parentId) AS (   select id,name,0,ParentId   from table_relation_1   where ParentId is null   union all   select id,name ,generation+1,ChildId   from table_relation_1 nxg   inner join CTE on nxg.ParentId=

Simple Delegate Example

Simple Delegate Example using System; public delegate int Delegatmethod(int a,int b); public class Sampleclass {     public int Add(int x,int y)     {     return x+y;     }     public int Sun(int x,int y)     {     return x-y;     } } class Program {     public static void Main(string[] args)     {     Sampleclass ss=new Sampleclass();     Delegatmethod delegate1=ss.Add;     int i=delegate1(10,20);     Console.WriteLine(i);     }     }

Calculating the sum of all the nodes in a path which is equal to a given number in a tree and to return if such a path exists

Calculating the sum of all the nodes in a path which is equal to a given number in a tree  and to return if such a path exists The idea is to run a recursive function and a stack to put the nodes from root to the leaf and pop one node at a time after calculating the sum if it is not equal to the supplied value.Then again put the other child node and check the sum.    using System; using System.Collections.Generic; class Node     {     public int data;     public Node Left;     public Node Right;     public Node(int initial_value)     {         data=initial_value;         Left=null;         Right=null;     }         } class Tree     {     Node top;     int sum;     int temp;     Stack<int> stk;     public Tree()     {     top=null;     temp=0;     sum=0;     stk=new Stack<int>();     }     public Tree(int initial_value)     {     top=new Node(initial_value);     }         public Node ReturnRoot()     {     return top;     }     public void AddNode(int initial_data)   

Tree mirror.

Tree mirroring - implementation in c# The  idea is to swap the side left child with the right child in a tree using System; class Node {     public int data;     public Node left;     public Node right;     public Node(int initial_value)     {     data=initial_value;     left=null;     right=null;     } } class Tree {     public Node top;     public Tree()     {     top=null;     }     public Node getTreeNode()     {     return top;     }         public Tree(int initial_value)     {     Node newnode=new Node(initial_value);     top=newnode;     }         public void AddRc(int data)     {     AddR(ref top,data);     }         public void AddR(ref Node N,int data)     {         if(N==null)         {         Node newnode=new Node(data);         N=newnode;         return;         }         if(data>N.data)         {         AddR(ref N.right,data);         return;         }   

Simple Tree Implementation in C#

   Simple Tree Implementation in C# using System; class Node     {     public int value;     public Node left;     public Node right;         public Node(int initial)     {     value =initial;     left= null;     right=null;     }     }  class tree     {     int count;     Node top;     public tree()     {     top=null;     count=0;     }     public tree(int initial)     {     top=new Node(initial);     }     public  void Add(int value)     {         if(top==null)         {         Node newnode=new Node(value);         top=newnode;         return;         }         Node currentnode =top;         bool added=false;                 do         {             if(value<currentnode.value)             {             if (currentnode.left==null)             {             Node newnode =new Node(value);             currentnode.left=newnode;             added=true;             }             else             {             currentnode=currentnode.left;             }             }             i
Traversing a tree in Inorder reversal Below is the code for reversing an inorder traversal of a tree using System; class node {     public int data;     public node left;     public node right;     public node(int value_initial)     {     data=value_initial;     left=null;     right=null;     } } class tree {     node top;     int count;     public int element;     public tree()     {     top=null;     count=0;     element=0;     }     public tree(int value_initial)     {     top =new node(value_initial);     }     public void AddR(int value_initial)     {     AddRc(ref top, value_initial);     }     private void AddRc(ref node N, int value_initial)     {            if(N==null)         {         node newnode=new node(value_initial);         N=newnode;         return;         }         if(value_initial < N.data)         {         AddRc(ref N.left,value_initial);         return;         }         if(value_initial >= N.data)         {         AddRc(ref N.right,value_initial)
Simple Graph implementation The idea is to implement a graph with a adjacency list and traverse the same in DFS using System; using System.Collections.Generic; public class Graph {     private List<int>[] childnodes;     public Graph(int size)     {     this.childnodes=new List<int>[size];     for(int i=0 ;i<size;i++)     {     this.childnodes[i]=new List<int>();     }     }     public Graph(List <int>[] childnodes)     {     this.childnodes=childnodes;     }     public int Size     {     get     {     return this.childnodes.Length;     }     }         public void AddEdge(int u, int v)     {     childnodes[u].Add(v);     }     public void RemoveEdge(int u,int v)     {     childnodes[u].Remove(v);     }     public bool hasEdge(int u,int v)     {     int vari=childnodes[u].IndexOf(v);     if(vari==-1)     return false;     else     return true;     }         public IList<int> getSuccessors(int v)     {     return childnodes[v];     }     } class Gra
C# .NET  Display Progress bar during data binding The below example and code is writing considering visual studio 2012 and SQL Server 2008 Express Edition.  Prerequisite  1) Download AjaxControlToolkit.Binary.NET40 and install it to the ToolBox in Visual Studio .       <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> 2) Create a database table "Test_data"   3) In Web.config file add the below section of code for connection string under configuration section     <connectionStrings>     <add name="1-ClickPublishConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=&  quot;C:\Users\Apotheosis\Documents\Visual Studio 2012\Projects\1-clickPublish\1-clickPublish\App_Data\1-ClickPublish.mdf&quot;;Integrated Security=True;Connect Timeout=30"       providerName="System.Data.SqlClient" />   </connectionStri