# 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 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}"); |
| } |
| |
| |
| for (int j = rowStartIdx; j < rowStartIdx + rowLength; j++) |
| { |
| for (int i = colStartIdx; i < colStartIdx + colLength; 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) |
| { |
| |
| 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) |
| { |
| |
| 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) |
| { |
| |
| 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) |
| { |
| |
| 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) |
| { |
| |
| 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 = 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
DataTableExtensions\DataTableExtensions.sln
DataTableExtensions\run.bat
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" |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2023-09-23 vscode vim config