20 June 2014

Take backup all database from sql server using cursors



DECLARE @name VARCHAR(50) -- database name 
DECLARE @path VARCHAR(256) -- path for backup files 
DECLARE @fileName VARCHAR(256) -- filename for backup 
DECLARE @fileDate VARCHAR(20) -- used for file name

SET @path = 'C:\Backup\' 

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR FOR 
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb') 

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  

WHILE @@FETCH_STATUS = 0  
BEGIN  
       SET @fileName = @path + @name + '_' + @fileDate + '.BAK' 
       BACKUP DATABASE @name TO DISK = @fileName 

       FETCH NEXT FROM db_cursor INTO @name  
END  

CLOSE db_cursor  
DEALLOCATE db_cursor

How to remove duplicate records from datatable in c#



 private DataTable RemoveDuplicatesRecords(DataTable dt)
 {
    var UniqueRows = dt.AsEnumerable().Distinct(DataRowComparer.Default);
    DataTable dt2 = UniqueRows.CopyToDataTable();
    return dt2;
  }

19 June 2014

Difference between a Class and an object ?


Class: 
1)It is a datatype that contains the programming logic. 
2)Class is visible in the source code and resides in hard disk. 
3)Class is like a template or blueprint of the object. It implements reusability, 
encapsulation, inheritance 
example:Button is a class with properties like Text,BackColor, 
events like click, methods like Focus 

Object: 
1)it is a chunk of memory that implements the class logic. 
2)Object is in the RAM and not visible in source code. 
3)It is the real world implementation of the class. 
Each object has its own copy of data. 
example: Button1, Button2 are the objects of Button class.

Difference between Value Type and Reference Type ?




Value type
Reference type
Value type they are stored on stack
Reference type they are stored on heap
When passed as value type new copy is created and passed so changes to variable does not get reflected back
When passed as Reference type then reference of that variable is passed so changes to variable does get reflected back
Value type store real data
Reference type store reference to the data.
Value types are faster in access
Reference types are slower in access.
Value types derive fromSystem.ValueType
Reference types derive fromSystem.Object
Value type consists of primitive data types, structures, enumerations.
Reference type consists of class, array, interface, delegates
Value types can not contain the valuenull.
Reference types can contain the valuenull.

Converting Array to List



using System.Linq;

List<datatype> list = array.ToList();

Comparing two datatables and returning a datatable by matching rows in c#.net using linq



  public DataTable get_Comparing(DataTable dt1, DataTable dt2)
   {
      DataTable dtMerged = (from a in dt1.AsEnumerable()
                                  join b in dt2.AsEnumerable()
                            on a["Student_Id"].ToString() equals b["Student_Id"].ToString()
                                  into g
                                  where g.Count() > 0
                                  select a).CopyToDataTable();

            return dtMerged;
   }