Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# datatable 查询

作者:萫蕉姺森う   发布日期:2026-01-12   浏览:98

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"]}");
        }
    }
}

解释说明:

  1. 创建DataTable:首先,我们创建了一个名为tableDataTable对象,并定义了三列:IDNameAge
  2. 添加数据:使用table.Rows.Add方法向DataTable中添加了几行数据。
  3. 查询数据:通过table.Select方法,我们可以根据指定的条件(如Age > 30)查询符合条件的行。Select方法返回一个DataRow数组。
  4. 输出结果:最后,我们遍历查询结果并将其打印到控制台。

这个示例展示了如何在C#中使用DataTable进行简单的查询操作。

上一篇:c# 运算符

下一篇:c# rectangle

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站