日常生活的交流与学习

首页 新随笔 联系 管理

# DataTableExtensions\App\App.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DataTablePrettyPrinter" Version="0.2.0" />
  </ItemGroup>

</Project>

DataTableExtensions\App\DataTableExtensions.cs


using System;
using System.Data;
using System.Data.Common;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography;

namespace Extensions;

public static class DataTableExtension
{
    public static bool IsEmpty(this DataTable dt)
    {
        if (dt.Rows.Count == 0)
        {
            return true;
        }
        return false;
    }

    public static DataTable GetDerivedTable(
        this DataTable parentDt,
        int rowStartIdx,
        int rowLength,
        int colStartIdx,
        int colLength
    )
    {
        // 创建一个新的 DataTable,只包含所需的列
        DataTable derivedTable = new DataTable();
        for (int i = 0; i < rowLength; i++)
        {
            DataRow newRow = derivedTable.NewRow();
            derivedTable.Rows.Add(newRow);
        }
        for (int i = 0; i < colLength; i++)
        {
            derivedTable.Columns.Add($"col_{i}");
        }
        // System.Console.WriteLine(derivedTable.ToPrettyPrintedString());
        // 将数据从原始 DataTable 复制到新的 DataTable
        for (int j = rowStartIdx; j < rowStartIdx + rowLength; j++)
        {
            for (int i = colStartIdx; i < colStartIdx + colLength; i++)
            {
                // System.Console.WriteLine($"{j},{i}");
                try
                {
                    derivedTable.Rows[j - rowStartIdx][i - colStartIdx] = parentDt.Rows[j][i];
                }
                catch (System.Exception ex)
                {
                    throw new IndexOutOfRangeException(
                        $"(row,col) = ({j},{i})超出datatable的索引范围:{ex.Message}"
                    );
                }
            }
        }
        return derivedTable;
    }

    public static string FirstRow(this DataRowCollection rows, string colName)
    {
        try
        {
            return rows[0][colName].ToString().Trim();
        }
        catch (System.Exception ex)
        {
            // System.Console.WriteLine(ex.Message);
            throw new Exception($"row[0][{colName}]元素不存在 : {ex.Message}");
        }
    }

    public static string FirstRow(this DataRowCollection rows, int colIdx)
    {
        try
        {
            return rows[0][colIdx].ToString().Trim();
        }
        catch (System.Exception ex)
        {
            // System.Console.WriteLine(ex.Message);
            throw new Exception($"row[0][{colIdx}]元素不存在 : {ex.Message}");
        }
    }

    public static string FirstCell(this DataTable dt)
    {
        try
        {
            return dt.Rows[0][0].ToString().Trim();
        }
        catch (Exception ex)
        {
            // System.Console.WriteLine(ex.Message);
            throw new Exception($"row[0][0]元素不存在 : {ex.Message}");
        }
    }

    public static string Get(this DataRow row, string colName)
    {
        try
        {
            return row[colName].ToString().Trim();
        }
        catch (Exception ex)
        {
            // System.Console.WriteLine(ex.Message);
            throw new Exception($"row[{colName}]元素不存在 : {ex.Message}");
        }
    }

    public static List<string> Get(this DataRow row, int count)
    {
        try
        {
            var resutls = new List<string>();
            for (int i = 0; i < count; i++)
            {
                resutls.Add(row[i].ToString());
            }
            return resutls;
        }
        catch (Exception ex)
        {
            // System.Console.WriteLine(ex.Message);
            throw new Exception($"row[]元素不存在 : {ex.Message}");
        }
    }

    static (string, dynamic) CreateTuple(List<object> list, int index)
    {
        if (index == list.Count - 1)
        {
            return (list[index].ToString(), null);
        }
        else
        {
            return (list[index].ToString(), CreateTuple(list, index + 1));
        }
    }
}

DataTableExtensions\App\Program.cs


using System;
using System.Data;
using DataTablePrettyPrinter;
using Extensions;

class Program
{
    static void Main()
    {
        DataTable dataTable = NewMethod();
        Console.WriteLine(dataTable.ToPrettyPrintedString());
        Console.WriteLine(dataTable.FirstCell());
        Console.WriteLine("----------------");
        Console.WriteLine(dataTable.Rows.FirstRow(1));
        Console.WriteLine("----------------");
        Console.WriteLine(dataTable.IsEmpty());
        Console.WriteLine("----------------");
        foreach (DataRow row in dataTable.Rows)
        {
            List<string> values = row.Get(3);
            foreach (var value in values)
            {
                Console.WriteLine(value);
            }
            Console.WriteLine("------------");
        }
    }

    private static DataTable NewMethod()
    {
        // 创建一个DataTable对象
        DataTable dataTable = new DataTable("MyDataTable");

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

        // 添加数据行
        DataRow row1 = dataTable.NewRow();
        row1["Id"] = 1;
        row1["Name"] = "Alice";
        row1["Age"] = 25;
        dataTable.Rows.Add(row1);

        DataRow row2 = dataTable.NewRow();
        row2["Id"] = 2;
        row2["Name"] = "Bruce";
        row2["Age"] = 30;
        dataTable.Rows.Add(row2);
        return dataTable;
    }
}

DataTableExtensions\App\run.bat

dotnet run

DataTableExtensions\DataTableExtensions.sln

DataTableExtensions\run.bat

cd App
dotnet run

DataTableExtensions\zz.bat

set "scriptPath=%cd%" 
D: && cd D:\dotnet.-script\App\bin\Debug\net8.0\ && D:\dotnet.-script\App\bin\Debug\net8.0\App.exe  "%scriptPath%"   "Question" "other"
posted on 2024-09-23 22:26  lazycookie  阅读(3)  评论(0编辑  收藏  举报