无法将System.Data.Datarow转换为System.DateTime

时间:2019-02-25 05:28:47

标签: c# entity-framework type-conversion

我有一个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);

但是当我要将这些行添加到Entity Framework表中时,就会出现错误。

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

我应该如何转换它们?

1 个答案:

答案 0 :(得分:1)

尝试一下。

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