c# - 如何在 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 方法。
要添加它,您可以使用 PM 控制台进行添加:
Install-Package System.Data.DataSetExtensions -Version 4.5.0-preview1-26216-02
这是将值设置为 DataRow 对象中的字段的最佳做法吗
在这种情况下,设置或调用方法都没有关系。在引擎盖下SetField正在做完全相同的事情,仅此而已。
推荐阅读
- java - 文件中没有这样的文件或目录错误(con,“r”):无法打开连接,您对此错误有什么解决方案?
- java - .length of int[] 如何在 java 中工作?
- c# - 角色不会旋转回原来的位置
- r - 使用最大似然的回归模型中的误差
- javascript - 如何通过表单标签包装输入按钮?
- r - map_dfr 并根据 R 中的列将行绑定在一起
- java - 在 Spring Boot 应用程序中使用 Filter 修改 ServletRequest
- reactjs - 无法让 `lodash.debounce()` 正常工作?多次执行......(反应,lodash,钩子)
- c - 如何在不使用的情况下实现功能
? - css - CSS中的calc函数多久执行一次?