首页 > 解决方案 > 无法将 System.Data.Datarow 转换为 System.DateTime

问题描述

我有一个 Datatable 和 DataRows,这些 DataRows 从 XML 文件中获取值。

DataRow row = table.NewRow();
row["InvoiceNumber"] = xmlData.ChildNodes[2].ChildNodes[0].ChildNodes[2].ChildNodes[0].InnerText;
row["InvoiceIssueDate"] = Convert.ToDateTime(xmlData.ChildNodes[2].ChildNodes[0].ChildNodes[2].ChildNodes[1].InnerText);
row["InvoiceDeliveryDate"] = Convert.ToDateTime(xmlData.ChildNodes[2].ChildNodes[0].ChildNodes[2].ChildNodes[2].InnerText);

但是当我想将这些行添加到我的实体框架表中时,就会出现错误。

Invoices invoices = new Invoices();
invoices.InvoiceNumber = table.Rows[0].ToString();
invoices.InvoiceIssueDate = table.Rows[1];
invoices.InvoiceDeliveryDate = table.Rows[2];

我应该如何转换它们?

标签: c#entity-frameworktype-conversion

解决方案


试试这个。

invoices.InvoiceIssueDate = Convert.ToDateTime(table.Rows[1]);
invoices.InvoiceDeliveryDate = Convert.ToDateTime(table.Rows[2]);

推荐阅读