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();
}
}
}
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment