ADO.NET DataSet Class | C# DataSet | Easy ADO.Net Tutorials | DataSet C# | ADO.NET

 using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data.SqlClient;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data;


namespace DataSet_ADO

{

    internal class Program

    {

        static void Main(string[] args)

        {

            try

            {

                //Performing DataTable and DataSet simultaneously without Stored Procedure as shown below :

                string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

                SqlConnection con = new SqlConnection(cs);

                string emp_query = "select * from employee_tbl";

                string std_query = "select * from student_tbl";

                SqlDataAdapter sda1 = new SqlDataAdapter(emp_query,con);

                SqlDataAdapter sda2 = new SqlDataAdapter(std_query,con);

                DataTable employees = new DataTable();

                DataTable students = new DataTable();

                sda1.Fill(employees);

                sda2.Fill(students);

                DataSet ds = new DataSet();


                ds.Tables.Add(employees);

                ds.Tables.Add(students);


                foreach(DataRow row in ds.Tables[0].Rows)

                {

                    Console.WriteLine(row[0] + "  " + row[1] + "  " + row[2] + "  " + row[3] + "  " + row[4] + "  " + row[5]);

                }

                Console.WriteLine("-----------------------------------------------");

                foreach(DataRow row in ds.Tables[1].Rows)

                {

                    Console.WriteLine(row[0] + "  " + row[1] + "  " + row[2] + "  " + row[3] + "  " + row[4]);

                }






                //Performing StoredProcedure without DataTable as shown below :

                //string query = "spGetEmployee";

                //SqlDataAdapter sda = new SqlDataAdapter(query,con);

                //sda.SelectCommand.CommandType = CommandType.StoredProcedure;

                //DataSet ds = new DataSet();

                //sda.Fill(ds);

                ////Tables will store in DataSet based on index(Here First table will store at index 0 and Second table will store at index 1)


                //ds.Tables[0].TableName = "employee_tbl";

                //ds.Tables[1].TableName = "student_tbl";


                //foreach (DataRow row in ds.Tables["employee_tbl"].Rows)

                //{

                //    Console.WriteLine(row[0] + "  " + row[1] + "  " + row[2] + "  " + row[3] + "  " + row[4] + "  " + row[5]);

                //}

                //Console.WriteLine("---------------------------------------");

                //foreach(DataRow row in ds.Tables["student_tbl"].Rows)

                //{

                //    Console.WriteLine(row[0] + "  " + row[1] + "  " + row[2] + "  " + row[3] + "  " + row[4]);

                //}


            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();

        }

    }

}

Above File is DataSet_ADO\Program.cs











<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=.\SQLEXPRESS;initial Catalog=ado_db;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Above File is DataSet_ADO\App.config






Comments