C# 中的数据集

王林
发布: 2024-09-03 15:05:17
原创
325 人浏览过

DataSet 是一个断开的架构,它表示表结构中的数据,这意味着数据分为行和列。数据集是数据库的本地副本,存在于本地系统中,使应用程序执行得更快、更可靠。 DataSet 的工作方式就像一个真正的数据库,拥有一整套数据,其中包括约束、表之间的关系等。它将在命名空间“System.数据”。

语法:

DataSet的语法如下所示,

public class DataSet : System.ComponentModel.MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISerializable, IXmlSerializable
{
}
登录后复制

数据集如何工作?

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

创建数据表 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,将列添加到工资表中,然后创建两个数据行并将这些数据行添加到 Salary 表中。
然后创建数据表 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 与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) 与上面相同,它初始化系统的新实例。数据。 DataSet 类提供序列化信息和上下文。它包含两个参数,其中信息是使它们序列化或反序列化对象的数据。上下文表示从源到目标的给定序列化流。
  • DataSet(SerializationInfo info, StreamingContext context, bool ConstructSchema) 与上面相同,它初始化 System.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();
}
}
}
登录后复制

输出:

C# 中的数据集

结论 – C# 中的数据集

在本文中,我解释了 C# 中的 DataSet,这是一种断开连接的架构,有助于更快、更可靠地使用应用程序。我希望这篇文章可以帮助您从编程和理论上理解 DataSet 的工作流程。

以上是C# 中的数据集的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!