成人欧美一区二区三区的电影,日韩一级一欧美一级国产,国产成人国拍亚洲精品,无码人妻精品一区二区三区毛片,伊人久久无码大香线蕉综合

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# 的DataTable類使用方法精簡匯總

admin
2025年10月18日 0:11 本文熱度 375

目錄

一、DataTable概述

1.創(chuàng)建 DataTable

2.添加行

3.修改行

4.刪除行

5.查詢行

6.排序行

7.合并 DataTable

8.克隆 DataTable

9.復(fù)制 DataTable

10.使用 DataView 過濾和排序

11.使用 DataTable 的事件

12.使用 DataTable 的約束

13.使用 DataTable 的表達式列

14.使用 DataTable 的 XML 序列化

15.使用 DataTable 的 JSON 序列化

二、總結(jié)



一、DataTable概述

C# 中的?DataTable?是一個非常重要的類,用于在內(nèi)存中存儲和操作數(shù)據(jù)。它類似于數(shù)據(jù)庫中的表,具有行和列的結(jié)構(gòu)。下面是一個詳細的教程,涵蓋了?DataTable?的常見操作方法,并提供了相應(yīng)的示例代碼。

1.創(chuàng)建 DataTable

首先,我們需要創(chuàng)建一個?DataTable?對象,并為其添加列。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ??// 創(chuàng)建 DataTable? ? ? ? DataTable table =?new?DataTable("MyTable");
? ? ? ??// 添加列? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ??// 打印表結(jié)構(gòu)? ? ? ??foreach?(DataColumn column?in?table.Columns)? ? ? ? {? ? ? ? ? ? Console.WriteLine(column.ColumnName +?" - "?+ column.DataType);? ? ? ? }? ? }}

2.添加行

我們可以使用?NewRow()?方法創(chuàng)建新行,并將其添加到?DataTable?中。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ??// 添加行? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 打印行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?table.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

3.修改行

我們可以通過索引或條件查找行,并修改其數(shù)據(jù)。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 修改行數(shù)據(jù)? ? ? ? DataRow rowToUpdate = table.Rows[0];? ? ? ? rowToUpdate["Name"] =?"Alice Smith";? ? ? ? rowToUpdate["Age"] =?26;
? ? ? ??// 打印修改后的行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?table.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

4.刪除行

我們可以通過?Remove()?或?Delete()?方法刪除行。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 刪除行? ? ? ? table.Rows[0].Delete();?// 標(biāo)記為刪除? ? ? ? table.AcceptChanges(); ?// 提交刪除
? ? ? ??// 打印剩余行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?table.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

5.查詢行

我們可以使用?Select()?方法查詢符合條件的行。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 查詢行? ? ? ? DataRow[] rows = table.Select("Age > 26");
? ? ? ??// 打印查詢結(jié)果? ? ? ??foreach?(DataRow row?in?rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

6.排序行

我們可以使用?DefaultView.Sort?屬性對行進行排序。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 排序? ? ? ? table.DefaultView.Sort =?"Age DESC";
? ? ? ??// 打印排序后的行數(shù)據(jù)? ? ? ??foreach?(DataRowView rowView?in?table.DefaultView)? ? ? ? {? ? ? ? ? ? DataRow row = rowView.Row;? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

7.合并 DataTable

我們可以使用?Merge()?方法合并兩個?DataTable。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table1 =?new?DataTable("MyTable");? ? ? ? table1.Columns.Add("ID",?typeof(int));? ? ? ? table1.Columns.Add("Name",?typeof(string));? ? ? ? table1.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table1.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table1.Rows.Add(row1);
? ? ? ? DataTable table2 =?new?DataTable("MyTable");? ? ? ? table2.Columns.Add("ID",?typeof(int));? ? ? ? table2.Columns.Add("Name",?typeof(string));? ? ? ? table2.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row2 = table2.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table2.Rows.Add(row2);
? ? ? ??// 合并 DataTable? ? ? ? table1.Merge(table2);
? ? ? ??// 打印合并后的行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?table1.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

8.克隆 DataTable

我們可以使用?Clone()?方法克隆?DataTable?的結(jié)構(gòu)。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table1 =?new?DataTable("MyTable");? ? ? ? table1.Columns.Add("ID",?typeof(int));? ? ? ? table1.Columns.Add("Name",?typeof(string));? ? ? ? table1.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table1.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table1.Rows.Add(row1);
? ? ? ??// 克隆 DataTable? ? ? ? DataTable table2 = table1.Clone();
? ? ? ??// 打印克隆后的表結(jié)構(gòu)? ? ? ??foreach?(DataColumn column?in?table2.Columns)? ? ? ? {? ? ? ? ? ? Console.WriteLine(column.ColumnName +?" - "?+ column.DataType);? ? ? ? }? ? }}

9.復(fù)制 DataTable

我們可以使用?Copy()?方法復(fù)制?DataTable?的結(jié)構(gòu)和數(shù)據(jù)。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table1 =?new?DataTable("MyTable");? ? ? ? table1.Columns.Add("ID",?typeof(int));? ? ? ? table1.Columns.Add("Name",?typeof(string));? ? ? ? table1.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table1.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table1.Rows.Add(row1);
? ? ? ??// 復(fù)制 DataTable? ? ? ? DataTable table2 = table1.Copy();
? ? ? ??// 打印復(fù)制后的行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?table2.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

10.使用 DataView 過濾和排序

DataView?是?DataTable?的一個視圖,可以用于過濾和排序數(shù)據(jù)。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?30;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 創(chuàng)建 DataView? ? ? ? DataView view =?new?DataView(table);? ? ? ? view.RowFilter =?"Age > 26";? ? ? ? view.Sort =?"Name DESC";
? ? ? ??// 打印過濾和排序后的行數(shù)據(jù)? ? ? ??foreach?(DataRowView rowView?in?view)? ? ? ? {? ? ? ? ? ? DataRow row = rowView.Row;? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

11.使用 DataTable 的事件

DataTable?提供了多個事件,如?RowChanged,?RowChanging,?RowDeleted,?RowDeleting?等,可以在數(shù)據(jù)發(fā)生變化時觸發(fā)。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ??// 訂閱事件? ? ? ? table.RowChanged +=?new?DataRowChangeEventHandler(RowChangedEvent);
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);? ? }
? ??private?static?void?RowChangedEvent(object?sender, DataRowChangeEventArgs e)? ? {? ? ? ? Console.WriteLine($"Row changed:?{e.Action},?{e.Row["Name"]}");? ? }}

12.使用 DataTable 的約束

我們可以為?DataTable?添加約束,如主鍵約束、唯一約束等。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ??// 添加主鍵約束? ? ? ? table.PrimaryKey =?new?DataColumn[] { table.Columns["ID"] };
? ? ? ??// 添加唯一約束? ? ? ? table.Columns["Name"].Unique =?true;
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ??// 嘗試添加重復(fù)主鍵? ? ? ??try? ? ? ? {? ? ? ? ? ? DataRow row2 = table.NewRow();? ? ? ? ? ? row2["ID"] =?1;?// 重復(fù)主鍵? ? ? ? ? ? row2["Name"] =?"Bob";? ? ? ? ? ? row2["Age"] =?30;? ? ? ? ? ? table.Rows.Add(row2);? ? ? ? }? ? ? ??catch?(Exception ex)? ? ? ? {? ? ? ? ? ? Console.WriteLine(ex.Message);? ? ? ? }? ? }}

13.使用 DataTable 的表達式列

我們可以使用表達式列來計算列的值。

using?System;using?System.Data;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ??// 添加表達式列? ? ? ? table.Columns.Add("IsAdult",?typeof(bool),?"Age >= 18");
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ? DataRow row2 = table.NewRow();? ? ? ? row2["ID"] =?2;? ? ? ? row2["Name"] =?"Bob";? ? ? ? row2["Age"] =?16;? ? ? ? table.Rows.Add(row2);
? ? ? ??// 打印表達式列的值? ? ? ??foreach?(DataRow row?in?table.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["Name"]}?is adult:?{row["IsAdult"]}");? ? ? ? }? ? }}

14.使用 DataTable 的 XML 序列化

我們可以將?DataTable?序列化為 XML,或者從 XML 反序列化為?DataTable。

using?System;using?System.Data;using?System.IO;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ??// 序列化為 XML? ? ? ??string?xml = table.GetXml();? ? ? ? Console.WriteLine(xml);
? ? ? ??// 將 XML 保存到文件? ? ? ? File.WriteAllText("table.xml", xml);
? ? ? ??// 從 XML 反序列化為 DataTable? ? ? ? DataTable newTable =?new?DataTable();? ? ? ? newTable.ReadXml("table.xml");
? ? ? ??// 打印反序列化后的行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?newTable.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

15.使用 DataTable 的 JSON 序列化

我們可以使用?JsonConvert?類將?DataTable?序列化為 JSON,或者從 JSON 反序列化為?DataTable。

using?System;using?System.Data;using?Newtonsoft.Json;
class?Program{? ??static?void?Main()? ? {? ? ? ? DataTable table =?new?DataTable("MyTable");? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ? DataRow row1 = table.NewRow();? ? ? ? row1["ID"] =?1;? ? ? ? row1["Name"] =?"Alice";? ? ? ? row1["Age"] =?25;? ? ? ? table.Rows.Add(row1);
? ? ? ??// 序列化為 JSON? ? ? ??string?json = JsonConvert.SerializeObject(table);? ? ? ? Console.WriteLine(json);
? ? ? ??// 從 JSON 反序列化為 DataTable? ? ? ? DataTable newTable = JsonConvert.DeserializeObject<DataTable>(json);
? ? ? ??// 打印反序列化后的行數(shù)據(jù)? ? ? ??foreach?(DataRow row?in?newTable.Rows)? ? ? ? {? ? ? ? ? ? Console.WriteLine($"{row["ID"]},?{row["Name"]},?{row["Age"]}");? ? ? ? }? ? }}

二、總結(jié)

DataTable?是 C# 中非常強大的數(shù)據(jù)結(jié)構(gòu),適用于處理內(nèi)存中的數(shù)據(jù)。通過本教程,我們應(yīng)該已經(jīng)掌握了?DataTable?的基本操作,包括創(chuàng)建、添加、修改、刪除、查詢、排序、合并、克隆、復(fù)制、使用?DataView、事件處理、約束、表達式列、XML 和 JSON 序列化等操作。


閱讀原文:原文鏈接


該文章在 2025/10/18 11:19:15 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved