微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 DataRow 对象中设置字段

如何解决如何在 DataRow 对象中设置字段

我已将旧代码从 vb 迁移到 c#。以下迁移的代码行引发错误

using System.Data;
..
..
DaTarow targetDaTarow = targetDataTable.NewRow();
targetDaTarow.SetField("DateValue",calendarRow.DateValue.ToShortDateString);

错误

=Error  CS1061  'DaTarow' does not contain a deFinition for 'SetField' and no accessible extension method 'SetField' accepting a first argument of type 'DaTarow' Could be found (are you missing a using directive or an assembly reference?)

我知道我可以像这样使用它:

targetDaTarow["DateValue"] = calendarRow.DateValue.ToShortDateString);

但是,这是将值设置为 DaTarow 对象中的字段的最佳做法吗?

解决方法

以下迁移的代码行引发错误:

 Error  CS1061  'DataRow' does not contain a definition for 'SetField' and no accessible extension method 'SetField' accepting a first argument of type 'DataRow' could be found (are you missing a using directive or an assembly reference?)

此错误是因为您尝试使用项目中未引用的 DataRowExtension.SetField Method

要添加它,您可以使用 PM 控制台:

 Install-Package System.Data.DataSetExtensions -Version 4.5.0-preview1-26216-02

这是将值设置为 DataRow 对象中的字段的最佳做法吗

在这种情况下,是设置还是调用方法都没有关系。在幕后,SetField 正在做完全相同的事情,仅此而已。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。