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

c# datatable

作者:Angel★狂龙   发布日期:2025-10-14   浏览:6

using System;
using System.Data;

class Program
{
    static void Main()
    {
        // 创建一个新的 DataTable 对象
        DataTable dt = new DataTable("Employees");

        // 添加列
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(int));

        // 添加行
        dt.Rows.Add(1, "Alice", 30);
        dt.Rows.Add(2, "Bob", 25);
        dt.Rows.Add(3, "Charlie", 35);

        // 打印 DataTable 的内容
        foreach (DataRow row in dt.Rows)
        {
            Console.WriteLine($"ID: {row["ID"]}, Name: {row["Name"]}, Age: {row["Age"]}");
        }
    }
}

解释说明:

  1. 创建 DataTable:使用 new DataTable("Employees") 创建一个名为 "Employees" 的 DataTable。
  2. 添加列:使用 dt.Columns.Add 方法添加三列,分别是 ID(整数类型)、Name(字符串类型)和 Age(整数类型)。
  3. 添加行:使用 dt.Rows.Add 方法向 DataTable 中添加三行数据。
  4. 遍历并打印:使用 foreach 循环遍历每一行,并使用 Console.WriteLine 打印每一行的数据。

如果你需要更多关于 C# 和 DataTable 的信息,请随时告诉我!

上一篇:c# substring

下一篇:c#多线程

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

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

Laravel 中文站