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 的工作方式就像一个真正的数据库,拥有一整套数据,其中包括约束、表之间的关系等。 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# 中的 DataSet,这是一种断开连接的架构,有助于更快、更可靠地使用应用程序。我希望这篇文章可以帮助您从编程和理论上理解 DataSet 的工作流程。
以上是C# 中的数据集的详细内容。更多信息请关注PHP中文网其他相关文章!