using System;
using System.Data;
public class DataTableQueryExample
{
public static void Main()
{
// 创建一个DataTable并添加一些数据
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Age", typeof(int));
// 添加行到DataTable
table.Rows.Add(1, "Alice", 30);
table.Rows.Add(2, "Bob", 25);
table.Rows.Add(3, "Charlie", 35);
// 查询年龄大于30的记录
string filterExpression = "Age > 30";
DataRow[] foundRows = table.Select(filterExpression);
// 输出查询结果
Console.WriteLine("查询结果:");
foreach (DataRow row in foundRows)
{
Console.WriteLine($"ID: {row["ID"]}, Name: {row["Name"]}, Age: {row["Age"]}");
}
}
}
table的DataTable对象,并定义了三列:ID、Name和Age。table.Rows.Add方法向DataTable中添加了几行数据。table.Select方法,我们可以根据指定的条件(如Age > 30)查询符合条件的行。Select方法返回一个DataRow数组。这个示例展示了如何在C#中使用DataTable进行简单的查询操作。
上一篇:c# 运算符
下一篇:c# rectangle
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站