Home > Article > Backend Development > Dataset in C#
DataSet is a disconnected architecture it represents the data in table structure which means the data into rows and columns. Dataset is the local copy of your database which exists in the local system and makes the application execute faster and reliable. DataSet works like a real database with an entire set of data which includes the constraints, relationship among tables, and so on. It will be found in the namespace “System. Data”.
Syntax:
The syntax of DataSet as shown below,
public class DataSet : System.ComponentModel.MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISerializable, IXmlSerializable { }
DataSet is a collection of data tables that contains the relational data in table structure. It signifies the subset of databases in memory management. The DataSet is a disconnected architecture that does not require an active or open connection to the database. In this way, we can obtain the data without interacting with any data source because of a disconnected environment. It belongs to the namespace System.Data. Let’s understand the working procedure of DataSet in C# with example, We creating two data tables Employee and Salary tables and then create data columns to add the columns into the tables and finally create data rows to add records into both the tables. Let’s see the following coding below,
Creating the DataTable EmployeeDetails
DataTable EmployeeDetails = new DataTable("EmployeeDetails"); //to create the column and schema DataColumn EmployeeID = new DataColumn("EmpID", typeof(Int32)); EmployeeDetails.Columns.Add(EmployeeID); DataColumn EmployeeName = new DataColumn("EmpName", typeof(string)); EmployeeDetails.Columns.Add(EmployeeName); DataColumn EmployeeMobile = new DataColumn("EmpMobile", typeof(string)); EmployeeDetails.Columns.Add(EmployeeMobile); //to add the Data rows into the EmployeeDetails table EmployeeDetails.Rows.Add(1001, "Andrew", "9000322579"); EmployeeDetails.Rows.Add(1002, "Briddan", "9081223457");
For the salary table we creating the DataTable with the name SalaryDetails with attributes SalaryID, EmployeeID, EmployeeName, and Salary add the columns to the salary tables and then create two data rows and add those data rows into Salary tables.
Then create the DataTable SalaryDetails,
DataTable SalaryDetails = new DataTable("SalaryDetails"); //to create the column and schema DataColumn SalaryId = new DataColumn("SalaryID", typeof(Int32)); SalaryDetails.Columns.Add(SalaryId); DataColumn empId = new DataColumn("EmployeeID", typeof(Int32)); SalaryDetails.Columns.Add(empId); DataColumn empName = new DataColumn("EmployeeName", typeof(string)); SalaryDetails.Columns.Add(empName); DataColumn SalaryPaid = new DataColumn("Salary", typeof(Int32)); SalaryDetails.Columns.Add(SalaryPaid); //to add the Data rows into the SalaryDetails table SalaryDetails.Rows.Add(10001, 1001, "Andrew",42000); SalaryDetails.Rows.Add(10002, 1002, "Briddan",30000);
To create the DataSet with DataTable,
As we discussed the DataSet with the collection of DataTables, then create object for DataSet and then add two data tables (Employee and Salary) into the DataSet.
//to create the object for DataSet DataSet dataSet = new DataSet(); //Adding DataTables into DataSet dataSet.Tables.Add(EmployeeDetails); dataSet.Tables.Add(SalaryDetails); By using index position, we can fetch the DataTable from DataSet, here first we added the Employee table so the index position of this table is 0, let's see the following code below //retrieving the DataTable from dataset using the Index position foreach (DataRow row in dataSet.Tables[0].Rows) { Console.WriteLine(row["EmpID"] + ", " + row["EmpName"] + ", " + row["EmpMobile"]); } Then second table we added was SalaryDetails table which the index position was 1, now we fetching this second table by using the name, so we fetching the DataTable from DataSet using the name of the table name "SalaryDetails", //retrieving DataTable from the DataSet using name of the table foreach (DataRow row in dataSet.Tables["SalaryDetails"].Rows) { Console.WriteLine(row["SalaryID"] + ", " + row["EmployeeID"] + ", " + row["EmployeeName"] + ", " + row["Salary"]); }
DataSet in C# provides four constructors, are as follows:
Dataset is the local copy of your database which exists in the local system and makes the application execute faster and reliable. DataSet works like a real database with an entire set of data which includes the constraints, relationship among tables, and so on. DataSet is a disconnected architecture it represents the data in table structure which means the data into rows and columns.
Let’s see the example programmatically as follows,
using System; using System.Collections.Generic; using System. Data; namespace Console_DataSet { class Program { static void Main(string[] args) { try { // building the EmployeeDetails table using DataTable DataTable EmployeeDetails = new DataTable("EmployeeDetails"); //to create the column and schema DataColumn EmployeeID = new DataColumn("EmpID", typeof(Int32)); EmployeeDetails.Columns.Add(EmployeeID); DataColumn EmployeeName = new DataColumn("EmpName", typeof(string)); EmployeeDetails.Columns.Add(EmployeeName); DataColumn EmployeeMobile = new DataColumn("EmpMobile", typeof(string)); EmployeeDetails.Columns.Add(EmployeeMobile); //to add the Data rows into the EmployeeDetails table EmployeeDetails.Rows.Add(1001, "Andrew", "9000322579"); EmployeeDetails.Rows.Add(1002, "Briddan", "9081223457"); // to create one more table SalaryDetails DataTable SalaryDetails = new DataTable("SalaryDetails"); //to create the column and schema DataColumn SalaryId = new DataColumn("SalaryID", typeof(Int32)); SalaryDetails.Columns.Add(SalaryId); DataColumn empId = new DataColumn("EmployeeID", typeof(Int32)); SalaryDetails.Columns.Add(empId); DataColumn empName = new DataColumn("EmployeeName", typeof(string)); SalaryDetails.Columns.Add(empName); DataColumn SalaryPaid = new DataColumn("Salary", typeof(Int32)); SalaryDetails.Columns.Add(SalaryPaid); //to add the Data rows into the SalaryDetails table SalaryDetails.Rows.Add(10001, 1001, "Andrew",42000); SalaryDetails.Rows.Add(10002, 1002, "Briddan",30000); //to create the object for DataSet DataSet dataSet = new DataSet(); //Adding DataTables into DataSet dataSet.Tables.Add(EmployeeDetails); dataSet.Tables.Add(SalaryDetails); Console.WriteLine("\n\n\tUSING DATASET"); Console.WriteLine("\n\nEmployeeDetails Table Data: \n"); //to reterieve the DataTable from dataset using the Index position foreach (DataRow row in dataSet.Tables[0].Rows) { Console.WriteLine(row["EmpID"] + ", " + row["EmpName"] + ", " + row["EmpMobile"]); } Console.WriteLine(); //SalaryDetails Table Console.WriteLine("\nOrderDetails Table Data: \n"); //retrieving DataTable from the DataSet using name of the table foreach (DataRow row in dataSet.Tables["SalaryDetails"].Rows) { Console.WriteLine(row["SalaryID"] + ", " + row["EmployeeID"] + ", " + row["EmployeeName"] + ", " + row["Salary"]); } } catch (Exception e) { Console.WriteLine("OOPS, Error.\n" + e); } Console.ReadKey(); } } }
Output:
In this article, I have explained DataSet in C# which is a disconnected architecture that helps to make use of the application faster and reliable. I hope the article helps you to understand the working flow of DataSet programmatically and theoretically.
The above is the detailed content of Dataset in C#. For more information, please follow other related articles on the PHP Chinese website!