简体   繁体   English

如何在 DataRow object 中设置字段

[英]How to set fields in DataRow object

I have migrated legacy code from vb to c#.我已将旧代码从 vb 迁移到 c#。 The following migrated line of code raises an error:以下迁移的代码行引发错误:

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

The error:错误:

=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?)

I know I can use it like:我知道我可以像这样使用它:

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

But, is this the best practice to set the value to a field in DataRow object?但是,这是将值设置为 DataRow object 中的字段的最佳做法吗?

The following migrated line of code raises an error:以下迁移的代码行引发错误:

 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?)

This error is because you're trying to use the DataRowExtension.SetField Method in which you don't have referenced in your project.此错误是因为您尝试使用项目中未引用的DataRowExtension.SetField 方法

To add it, you can do so using the PM console:要添加它,您可以使用 PM 控制台进行添加:

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

Is this the best practice to set the value to a field in DataRow object这是将值设置为 DataRow object 中的字段的最佳做法吗

It doesn't matter if you set it or call the method in this case.在这种情况下,设置或调用方法都没有关系。 Under the hood SetField is doing the exact same thing and nothing more.在引擎盖下SetField正在做完全相同的事情,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM