.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Merging Data Within Datasets

Posted By:Ravi Ranjan Kumar       Posted Date: June 24, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

This article enable you to learn how to merge one dataset to another dataset
 

Sometimes, the data available in one Datset can be merged with another datset,or a copy of the DataTable objects can be created so that the user can edit or modify data., which can be merged back to the original dataset. the Merge() method is used to combine data from multiple Dataset,DataTable and DataRow objects.
consider the following code snippet of merging data from one DataSet to another by using Merge() method :
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog = YourDatabaseName;uid =sa;pwd = YourPassword");
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "select *  from table1";
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            //create the first dataset
            DataSet ds1 = new DataSet();
            con.Open();
            da.Fill(ds1, "table1");
            cmd.CommandText = "select *  from table2";

            //create the second dataset
            DataSet ds2 = new DataSet();
            da.Fill(ds2, "table2");
            con.Close();

            //Merger the two dataset ds1 with dataset ds2
            ds1.Merge(ds2, true, MissingSchemaAction.Add);
            foreach (DataTable dt in ds1.Tables)
            {
                Console.WriteLine("\nReading frpm the " + dt + "Datatable");
                foreach (DataRow dr in dt.Rows)
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        Console.WriteLine(dc + "= " + dr[dc]);
                    }
                }
            }
            Console.ReadLine();
        }
In the preceding code snippet, the Merge() method accepts three parameters. the first parameter is the dataset whose data and schema will be merged. the second parameter accepts a boolean value, true to preserve changes in the current dataset, otherwise false. and the third parameter is the MissingSchemaAction enumeration. this specifies the action to be taken when the data is added to the dataset and the required DataTable or DataColumn is missing. 

Given SQL query used for this example to illustrate Merging DataSet 
create table table1(name varchar(15),address varchar(50))
create table table2(name varchar(15),Phone varchar(10),City varchar(10))

-- inserting value into table1
insert into table1 values('XYZ','add-1')
insert into table1 values('Name-2','add-2')
select * from table1

--inserting value into table2
insert into table2 values('Name-1','1234567891','City-1')
insert into table2 values('Name-2','1234567891','City-2')
insert into table2 values('Name-3','2514204664','City-3')
select * from table2
when you will run query your output will  be 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend