从 DataRow 填充 Nullable 类型

发布于 2024-10-01 11:30:20 字数 2451 浏览 1 评论 0原文

我想从 DataRow 中的项目获取值,并且希望将结果存储在可为空日期字段中。

我看到(在VB中)对象System.Data.DataRowExtensions上出现了共享“Field”函数。以下来自对象浏览器:

Public Shared Function Field(Of T)(ByVal row As System.Data.DataRow, ByVal column As System.Data.DataColumn, ByVal version As System.Data.DataRowVersion) As T
     Member of System.Data.DataRowExtensions
Summary:
Provides strongly-typed access to each of the column values in the specified row. The System.Data.DataRowExtensions.Field``1(System.Data.DataRow,System.Data.DataColumn,System.Data.DataRowVersion) method also supports nullable types.

Type parameters:
T: A generic parameter that specifies the return type of the column.

Parameters:
row: The input System.Data.DataRow, which acts as the this instance for the extension method.
column: The input System.Data.DataColumn object that specifies the column to return the value of.
version: A System.Data.DataRowVersion enumeration that specifies the version of the column value to return, such as Current or Original version.

但是,当我输入

System.Data.DataRowExtensions.Field (...)

"...Field 在智能感知中无法识别。

当我尝试从智能感知中引用 Field 函数时,智能感知中也没有列出 Field 函数。 DataRow 实例变量,我不希望这是这样做的方法,因为该函数是共享的

Dim MyDataRow As DataRow 。 MyDataRow.Field()

我希望能够执行以下操作:

For Each MyDataRow As DataRow in ds.tables(0).rows
       Dim nullableDate As System.Nullable(Of DateTime) = System.Data.DataRowExtensions.Field(Of System.Nullable(Of DateTime))("DateColInDb")
next 

或这样

For Each MyDataRow As DataRow in ds.tables(0).rows
       Dim nullableDate As System.Nullable(Of DateTime) = MyDataRow .Field(Of System.Nullable(Of DateTime))("DateColInDb")
next 

但是“System.Data.DataRowExtensions.Field”或“Field”函数无法识别。

编辑

当将数据从可空日期类型移回到数据行时,我是这样做的:

Dim rowTest As DataRow = dtbTest.NewRow

dtbTest.Rows.Add(rowTest)

rowTest.Item("MyDateTime") = Me.MyDateTime.ToObject

"ToObject" is a我添加到 Nullable Date 数据类型的自定义函数。

模块 NullableDateExtensions

<System.Runtime.CompilerServices.Extension()> _
Public Function ToObject(ByVal thisDate As Date?) As Object
    Return If(thisDate.HasValue, CType(thisDate, Object), DBNull.Value)
End Function

结束模块

注释?

有人有更好的方法将值移入数据行或从数据行移入可空类型吗?

..或者是否有一个内置的方便函数可以执行此操作,这样我就不必定义自己的函数?

I want to get a value from an item in a DataRow and I would like to store the result in a Nullable Date field.

I see (in VB) that there appears shared "Field" function on the object System.Data.DataRowExtensions. Here's from the Object Browser:

Public Shared Function Field(Of T)(ByVal row As System.Data.DataRow, ByVal column As System.Data.DataColumn, ByVal version As System.Data.DataRowVersion) As T
     Member of System.Data.DataRowExtensions
Summary:
Provides strongly-typed access to each of the column values in the specified row. The System.Data.DataRowExtensions.Field``1(System.Data.DataRow,System.Data.DataColumn,System.Data.DataRowVersion) method also supports nullable types.

Type parameters:
T: A generic parameter that specifies the return type of the column.

Parameters:
row: The input System.Data.DataRow, which acts as the this instance for the extension method.
column: The input System.Data.DataColumn object that specifies the column to return the value of.
version: A System.Data.DataRowVersion enumeration that specifies the version of the column value to return, such as Current or Original version.

However, when I type

System.Data.DataRowExtensions.Field (...)

"...Field is not recognized in intellisense.

Nor is the Field function listed in intellisense when I attempt to reference it from an DataRow instance variable, which I wouldn't expect to be the way to do it, since the function is shared.

Dim MyDataRow As DataRow
MyDataRow.Field()

I expected to be able to do something like this:

For Each MyDataRow As DataRow in ds.tables(0).rows
       Dim nullableDate As System.Nullable(Of DateTime) = System.Data.DataRowExtensions.Field(Of System.Nullable(Of DateTime))("DateColInDb")
next 

or this

For Each MyDataRow As DataRow in ds.tables(0).rows
       Dim nullableDate As System.Nullable(Of DateTime) = MyDataRow .Field(Of System.Nullable(Of DateTime))("DateColInDb")
next 

But the "System.Data.DataRowExtensions.Field" or "Field" function is not recognized.

EDIT

When moving data from a Nullable Date type BACK to a datarow, I am this is way I am doing:

Dim rowTest As DataRow = dtbTest.NewRow

dtbTest.Rows.Add(rowTest)

rowTest.Item("MyDateTime") = Me.MyDateTime.ToObject

"ToObject" is a custom function that I added to the Nullable Date datatype.

Module NullableDateExtensions

<System.Runtime.CompilerServices.Extension()> _
Public Function ToObject(ByVal thisDate As Date?) As Object
    Return If(thisDate.HasValue, CType(thisDate, Object), DBNull.Value)
End Function

End Module

Comments?

Does somebody have a better way to move values to and from a datarow into Nullable types?

..OR is there a built-in handy function to do this so I don't have to define my own?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2024-10-08 11:30:20

确保您已添加对 System.Data.DataSetExtensions.dll 的引用。转到解决方案资源管理器 ->参考文献->添加它。

然后这样使用:

For Each row As DataRow In ds.Tables(0).Rows
    Dim nullableDate = row.Field(Of Nullable(Of DateTime))("DateColInDb")
Next

您还可以使用 DateTime? 的简写形式来代替 Nullable(Of DateTime)

Make sure you've added a reference to System.Data.DataSetExtensions.dll. Go to Solution Explorer -> References -> add it.

Then use it in this way:

For Each row As DataRow In ds.Tables(0).Rows
    Dim nullableDate = row.Field(Of Nullable(Of DateTime))("DateColInDb")
Next

You can also use the shorthand form of DateTime? instead of Nullable(Of DateTime).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文