开发者

Populating a Nullable type from a DataRow

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜