C#中的数据集简介及实例

441 阅读5分钟

Dataset in C#

C#中的数据集简介

数据集是一种不相连的架构,它以表的结构表示数据,即把数据分为行和列。数据集是你的数据库的本地副本,它存在于本地系统中,使应用程序执行得更快更可靠。数据集的工作方式就像一个真正的数据库,拥有一整套数据,包括约束条件、表之间的关系等等。它可以在命名空间 "System.数据 "中找到。

语法。

DataSet的语法如下所示。

public class DataSet : System.ComponentModel.MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISerializable, IXmlSerializable
{
}

数据集如何工作?

DataSet是一个数据表的集合,包含表结构的关系数据。它标志着内存管理中的数据库子集。DataSet是一个断开连接的架构,不需要与数据库的活动或开放连接。通过这种方式,我们可以在不与任何数据源交互的情况下获得数据,因为是断开连接的环境。它属于System.Data命名空间。让我们通过例子来了解一下C#中DataSet的工作程序,我们创建了两个数据表Employee和Salary,然后创建数据列,将这些列添加到表中,最后创建数据行,将记录添加到这两个表中。让我们看看下面的编码。

创建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");

对于工资表,我们创建名为SalaryDetails的DataTable,其属性为SalaryID、EmployeeID、EmployeeName和Salary,将这些列添加到工资表中,然后创建两个数据行,将这些数据行添加到工资表中。
然后创建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);

用DataTable创建DataSet。

正如我们所讨论的DataSet与DataTables的集合,然后创建DataSet对象,再将两个数据表(Employee和Salary)添加到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"]);
}

在C#中,DataSet提供了四个构造函数,如下所示。

  • DataSet() 它派生自System.Data.DataSet类,并初始化一个新的类实例。
  • DataSet(String data SetName)它表示名称,它用名称初始化System.Data.DataSet类的新实例,它包含字符串参数dataSetName,指定System.Data.DataSet的名称。
  • DataSet(Serialization info, StreamingContext context)和上面一样,它初始化System.Data.DataSet类的新实例。它初始化了System.Data.DataSet的新实例。DataSet类的新实例,它给出了序列化信息和上下文。它包含两个参数,其中信息是使他们序列化或反序列化对象的数据。上下文表示从源头到目的地的给定序列化流。
  • DataSet(SerializationInfo info, StreamingContext context, bool ConstructSchema)和上面一样,它初始化了一个新的System.Data的实例。数据。DataSet类的新实例。

例子

数据集是你的数据库的本地拷贝,它存在于本地系统中,使应用程序执行得更快、更可靠。DataSet的工作方式就像一个真正的数据库,有一整套的数据,包括约束条件、表之间的关系等等。DataSet是一个不相连的架构,它以表的结构表示数据,即数据分为行和列。

让我们来看看下面这个例子的编程。

程序

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

输出。

dataset in C#

结论 - C#中的数据集

在这篇文章中,我解释了C#中的数据集,它是一种断开的架构,有助于更快、更可靠地使用应用程序。我希望这篇文章能够帮助你从程序上和理论上理解DataSet的工作流程。