C# 中的資料集

王林
發布: 2024-09-03 15:05:17
原創
238 人瀏覽過

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學習者快速成長!