Search This Blog

Wednesday 12 August 2015

Difference between DataSet.Copy() and DataSet.Clone() Or Difference between DataTable.Copy() and DataTable.Clone()

Step 1:- Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" Default.aspx.cs" Inherits="Demo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Copy vs. Clone</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

Step 2:- Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Demo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = GenerateData();

                DataTable dt1 = new DataTable();

                dt1 = dt.Copy();

                dt1 = dt.Clone();
            }

        }

        private DataTable GenerateData()
        {
            DataTable dt = new DataTable();

            DataRow dr = null;

            dt.Columns.Add(new DataColumn("Column1"typeof(string)));

            dt.Columns.Add(new DataColumn("Column2"typeof(string)));

            dt.Columns.Add(new DataColumn("Column3"typeof(string)));

            dt.Columns.Add(new DataColumn("Column4"typeof(DateTime)));

            int totalRow = 25;

            for (int i = 0; i < totalRow; i++)
            {
                dr = dt.NewRow();

                dr["Column1"] = i + 1;

                dr["Column2"] = "Row" + i;

                dr["Column3"] = "Row" + i;

                dr["Column4"] = DateTime.Now;

                dt.Rows.Add(dr);
            }

            return dt;
        }
    }
}

Step 3:-  dt1 = dt.Copy();

Result





Step 4:-  dt1 = dt.Clone();

Result





Note:-

1. DataSet.Copy() / DataTable.Copy() 

It copies both Structure and data.

2.  DataSet.Clone() / DataTable.Clone()   

It only copies the structure but does not Copies the Data.

OR

Clone only copies the schema of a DataSet object, whereas Copy copies both the structure and data of a DataSet object.

Copy = clone + copy data.
Clone = copy data schema


No comments:

Post a Comment