gpt4 book ai didi

C#:使用列日期对 DATATABLE 进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:16 25 4
gpt4 key购买 nike

我知道这是一个重复的问题,但我已经尝试了很多,但我遇到了异常

  • $exception {"String was not recognized as a valid DateTime."} System.Exception {System.FormatException}

以下是我的代码,请检查指导

SQL 查询

SELECT gangId as gang, respectPoints as respectPoints,DATE_FORMAT( purchasedDate, '%d-%m-%Y') as date_purchase FROM tbl_gang t where gangId=" + gangId

数据访问层代码

DataTable dt = new DataTable();
MySqlCommand cmd = conn.CreateCommand();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
cmd.CommandText = inAppPurchaseQuery;
da.Fill(dt)

排序代码

dt = gangRPObj.getGangRPLogsBL(gangId, fromDate, toDate);
var sortedTable = dt.AsEnumerable()
.OrderBy(r => DateTime.ParseExact(("date_purchase"),
"dd-mm-yyyy", null))
.CopyToDataTable();

谢谢

最佳答案

您正在将字符串传递到日期时间转换,这就是出现异常的原因。尝试

var orderedRows = from row in dt.AsEnumerable()
let date = DateTime.ParseExact(row.Field<string>("date_purchase"),"dd-mm-yyyy", null)
orderby date
select row;

关于C#:使用列日期对 DATATABLE 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28273329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com