【发布时间】:2014-10-10 06:15:46
【问题描述】:
我正在经历一个场景,我有 DataTable 并且其中有多种类型的列,我必须过滤掉 Null Valued Column 的数据类型,以便我可以为其分配一些默认值,现在没有什么可以陷阱。下面是测试代码。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim table As DataTable = GetTable()
'
' We can instantiate a new object array and add it as a row.
'
Dim row As DataRow = table.Rows(1)
For Each item As Object In row.ItemArray
If TypeOf item Is Integer Then
Console.WriteLine("Int: {0}", item)
ElseIf TypeOf item Is String Then
Console.WriteLine("String: {0}", item)
ElseIf TypeOf item Is DateTime Then
Console.WriteLine("DateTime: {0}", item)
ElseIf TypeOf item Is System.DBNull Then
Console.WriteLine("DBNULL {0}", item)
End If
Next
End Sub
Private Shared Function GetTable() As DataTable
' Here we create a DataTable with four columns.
Dim table As New DataTable()
table.Columns.Add("ID", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Desc", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
' Here we add five DataRows.
table.Rows.Add(1, "Abc", "Cool Down", DateTime.Now)
table.Rows.Add(2, "Chenno", "Helifire", DBNull.Value)
Return table
End Function
结束类
我只遍历空值列行,对于日期列,它显示 "System.DBNull" 。我需要弄清楚该列的数据类型。
【问题讨论】:
标签: vb.net datatable datarow dbnull