본문으로 바로가기

(vb.net) DataTable 예제

category vb.net 2014. 11. 28. 00:45

Module Module1

    Sub Main()
' Get a DataTable instance from helper function.
Dim table As DataTable = GetTable()
    End Sub

    ''' <summary>
    ''' Helper function that creates new DataTable.
    ''' </summary>
    Function GetTable() As DataTable
' Create new DataTable instance.
Dim table As New DataTable

' Create four typed columns in the DataTable.
table.Columns.Add("Dosage", GetType(Integer))
table.Columns.Add("Drug", GetType(String))
table.Columns.Add("Patient", GetType(String))
table.Columns.Add("Date", GetType(DateTime))

' Add five rows with those columns filled in the DataTable.
table.Rows.Add(25, "Indocin", "David", DateTime.Now)
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
Return table
 
    End Function

End Module



Module Module1

    Sub Main()
' This calls the GetTable method from above.
Dim table As DataTable = GetTable()

' Access Rows property on DataTable.
For Each row As DataRow In table.Rows
   ' Write value of first Integer.
   Console.WriteLine(row.Field(Of Integer)(0))
Next
    End Sub

End Module











Dim workRow As DataRow = workTable.NewRow()


workRow("CustLName") = "Smith"
workRow(1) = "Smith"

workTable.Rows.Add(workRow)





Dim workRow As DataRow
Dim i As Integer

For i = 0 To 9
  workRow = workTable.NewRow()
  workRow(0) = i
  workRow(1) = "CustName" & I.ToString()
  workTable.Rows.Add(workRow)
Next