Sponsored Ad

Wednesday, August 18, 2010

How to Remove and Add DataTables from DataSet

This example demonstrate a simple C# code to add and remove datatable from dataset. In this example 3 datatables are added to dataset and then two datatables have been removed from dataset by using two different methods. the output of this program is table3.

How to Remove and Add DataTables from DataSet

 

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class DeleteTables
    {
        public static void Main()
        {
            DataSet oDs = new DataSet();
            DataTable ot1 = new DataTable();
            DataTable ot2 = new DataTable();
            DataTable ot3 = new DataTable();

            //Add datatable into dataset
            oDs.Tables.Add(ot1);
            oDs.Tables.Add(ot2);
            oDs.Tables.Add(ot3);

            Console.WriteLine("Total tables in Dataset:");

            foreach (DataTable t in oDs.Tables)
                Console.WriteLine(t.TableName);

            oDs.Tables.Remove(ot2);
            oDs.Tables.RemoveAt(0);

            Console.WriteLine();
            Console.WriteLine("After removing tables in Dataset:");

            foreach (DataTable t in oDs.Tables)
                Console.WriteLine(t.TableName);

            Console.ReadLine();

        }
    }

}

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers