C# のデータセット

王林
リリース: 2024-09-03 15:05:17
オリジナル
239 人が閲覧しました

DataSet は、データをテーブル構造で表現する、つまりデータを行と列に分割する非接続アーキテクチャです。データセットは、ローカル システムに存在するデータベースのローカル コピーであり、アプリケーションの実行を高速かつ信頼性の高いものにします。 DataSet は、制約やテーブル間の関係などを含むデータのセット全体を扱う実際のデータベースのように機能します。これは、名前空間「System.データ」。

構文:

以下に示す DataSet の構文

public class DataSet : System.ComponentModel.MarshalByValueComponent, IListSource, ISupportInitializeNotification, ISerializable, IXmlSerializable
{
}
ログイン後にコピー

DataSet はどのように機能しますか?

DataSet は、テーブル構造のリレーショナル データを含むデータ テーブルのコレクションです。これは、メモリ管理におけるデータベースのサブセットを意味します。 DataSet は、データベースへのアクティブな接続や開いた接続を必要としない、非接続型のアーキテクチャです。このようにして、接続されていない環境のため、データ ソースと対話することなくデータを取得できます。これは名前空間 System.Data に属します。 C# での DataSet の作業手順を例として理解しましょう。従業員テーブルと給与テーブルという 2 つのデータ テーブルを作成し、次にデータ列を作成してその列をテーブルに追加し、最後にデータ行を作成して両方のテーブルにレコードを追加します。以下のコーディングを見てみましょう。

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");
ログイン後にコピー

給与テーブルの場合、属性 SalaryID、EmployeeID、EmployeeName、Salary を持つ SalaryDetails という名前の DataTable を作成し、給与テーブルに列を追加してから 2 つのデータ行を作成し、それらのデータ行を給与テーブルに追加します。
次に、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 と DataTable のコレクションについて説明したように、DataSet のオブジェクトを作成し、2 つのデータ テーブル (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 には、次の 4 つのコンストラクターが用意されています。

  • DataSet() System.Data.DataSet クラスから派生し、クラスの新しいインスタンスを初期化します。
  • DataSet(String data SetName) 名前を表し、System.Data.DataSet の名前を指定する文字列パラメーター dataSetName を含む名前で System.Data.DataSet クラスの新しいインスタンスを初期化します。
  • DataSet(Serialization info, StreamingContext context) は上記と同じで、システムの新しいインスタンスを初期化します。データ。シリアル化情報とコンテキストを提供する DataSet クラス。これには 2 つのパラメータが含まれており、情報はオブジェクトをシリアル化またはシリアル化解除するためのデータです。コンテキストは、ソースから宛先までの指定されたシリアル化されたストリームを表します。
  • 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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!