目錄
一、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()? ? {? ? ? ??? ? ? ? DataTable table =?new?DataTable("MyTable");
? ? ? ??? ? ? ? table.Columns.Add("ID",?typeof(int));? ? ? ? table.Columns.Add("Name",?typeof(string));? ? ? ? table.Columns.Add("Age",?typeof(int));
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ? DataRow rowToUpdate = table.Rows[0];? ? ? ? rowToUpdate["Name"] =?"Alice Smith";? ? ? ? rowToUpdate["Age"] =?26;
? ? ? ??? ? ? ??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();?? ? ? ? table.AcceptChanges(); ?
? ? ? ??? ? ? ??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");
? ? ? ??? ? ? ??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";
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ? table1.Merge(table2);
? ? ? ??? ? ? ??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 table2 = table1.Clone();
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ? DataTable table2 = table1.Copy();
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ? DataView view =?new?DataView(table);? ? ? ? view.RowFilter =?"Age > 26";? ? ? ? view.Sort =?"Name DESC";
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ??try? ? ? ? {? ? ? ? ? ? DataRow row2 = table.NewRow();? ? ? ? ? ? row2["ID"] =?1;?? ? ? ? ? ? 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);
? ? ? ??? ? ? ??string?xml = table.GetXml();? ? ? ? Console.WriteLine(xml);
? ? ? ??? ? ? ? File.WriteAllText("table.xml", xml);
? ? ? ??? ? ? ? DataTable newTable =?new?DataTable();? ? ? ? newTable.ReadXml("table.xml");
? ? ? ??? ? ? ??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);
? ? ? ??? ? ? ??string?json = JsonConvert.SerializeObject(table);? ? ? ? Console.WriteLine(json);
? ? ? ??? ? ? ? DataTable newTable = JsonConvert.DeserializeObject<DataTable>(json);
? ? ? ??? ? ? ??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 編輯過