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

c# dataset

作者:戮尽逆者   发布日期:2026-06-26   浏览:85

using System;
using System.Data;

class Program
{
    static void Main()
    {
        // 创建一个新的DataSet对象
        DataSet dataSet = new DataSet("MyDataSet");

        // 创建一个DataTable并添加到DataSet中
        DataTable table = new DataTable("Customers");
        dataSet.Tables.Add(table);

        // 添加列到DataTable
        table.Columns.Add("CustomerID", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("City", typeof(string));

        // 添加行到DataTable
        table.Rows.Add(1, "John Doe", "New York");
        table.Rows.Add(2, "Jane Smith", "Los Angeles");
        table.Rows.Add(3, "Sam Brown", "Chicago");

        // 遍历DataTable中的数据
        foreach (DataRow row in table.Rows)
        {
            Console.WriteLine($"CustomerID: {row["CustomerID"]}, Name: {row["Name"]}, City: {row["City"]}");
        }
    }
}

解释说明:

  1. 创建DataSetDataSet dataSet = new DataSet("MyDataSet"); 创建了一个名为 MyDataSetDataSet 对象。
  2. 创建DataTableDataTable table = new DataTable("Customers"); 创建了一个名为 CustomersDataTable,并将其添加到 DataSet 中。
  3. 添加列:使用 table.Columns.Add 方法为 DataTable 添加列,指定了列名和数据类型。
  4. 添加行:使用 table.Rows.Add 方法向 DataTable 中添加行数据。
  5. 遍历数据:使用 foreach 循环遍历 DataTable 中的每一行,并打印出每一行的数据。

这个示例展示了如何创建、填充和遍历 DataSetDataTable 中的数据。

上一篇:c#基础

下一篇:c#字符串拼接

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

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

Laravel 中文站