Dataset in C#

王林
Release: 2024-09-03 15:05:17
Original
387 people have browsed it

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
{
}
Copy after login

How DataSet Works?

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");
Copy after login

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);
Copy after login

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"]);
}
Copy after login

DataSet in C# provides four constructors, are as follows:

  • DataSet() it derives from the System.Data.DataSet class and initializes the new instance of a class.
  • DataSet(String data SetName) it represents the name and it initializes the new instance of the System.Data.DataSet class with the name it contains the string parameter dataSetName which specifies the name of the System.Data.DataSet.
  • DataSet(Serialization info, StreamingContext context) is the same as above it initialize the new instance of the System. Data. DataSet class which gives the serialization information and the context. It contains two parameters where the info is the data to make them serialize or de-serialize an object. The context represents the given serialized stream from source to destination.
  • DataSet(SerializationInfo info, StreamingContext context, bool ConstructSchema) is the same as above it initializes a new instance of System. Data. DataSet class.

Examples

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,

Program

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();
}
}
}
Copy after login

Output:

Dataset in C#

Conclusion – Dataset in C#

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template