HomeSearch

VB.NET DataTable Examples

Use DataTable to store data in memory from databases and other data sources.

DataTable.

Data is often stored in tables. And these tables often reside in databases. In the .NET Framework, the DataTable type stores data in memory.

Columns, rows.

Used often in VB.NET programs, DataTable has columns and rows properties. DataTable is an in-memory representation of structured data (like data read from a database).

A program.

We define GetTable—this returns a new DataTable. When the GetTable function is invoked, it creates a new DataTable and adds 4 columns to it.

Columns: These are named with a string argument and a Type argument. They have different types.

GetTable: In a DataTable, each column allows a specific type of data. The GetTable method adds 5 rows to the DataTable.

Tip: The arguments to the Rows.Add method are of the types specified in the columns.

VB.NET program that creates DataTable 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

Adding columns, rows.

By using the general pattern of adding columns and rows, you can construct usable DataTables in any program context.

Then: You can do more useful tasks such as storing them to SQL Server databases.

Also: You can display them on a DataGridView control in Windows Forms. DataTable is often used together with other System.Data types.

Rows.

Please paste in the GetTable method to run this program. In the For-Each loop, we loop over each Row. With Field(), we print the first Integer cell in each row.

Generic method: Field, part of DataRow, is a generic method. So we must specify the Of Integer part to indicate its parametric type.

VB.NET program that loops over rows 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 Output 25 50 10 21 100

Compare rows.

Suppose we have 2 DataTables and we wish to compare the rows in each. We can use a nested For-Each loop. And then we call SequenceEqual.

SequenceEqual: This is an extension method from LINQ. It tells us whether 2 arrays are the same.

Arrays

Note: We access ItemArray on each row, and then pass those arrays to SequenceEqual.

LINQ

Tip: For more complex rows, we could add a method that converts the Object array into an array of just the important elements.

VB.NET program that compares rows Module Module1 Function GetTable1() As DataTable Dim table As New DataTable table.Columns.Add("Size", GetType(Integer)) table.Columns.Add("Weight", GetType(Integer)) table.Rows.Add(25, 100) table.Rows.Add(100, 200) Return table End Function Function GetTable2() As DataTable Dim table As New DataTable table.Columns.Add("Size", GetType(Integer)) table.Columns.Add("Weight", GetType(Integer)) table.Rows.Add(300, 400) table.Rows.Add(25, 100) Return table End Function Sub Main() Dim table1 As DataTable = GetTable1() Dim table2 As DataTable = GetTable2() ' Loop over all rows. For Each row1 As DataRow In table1.Rows For Each row2 As DataRow In table2.Rows ' Get ItemArray for each row. Dim array1 As Object() = row1.ItemArray Dim array2 As Object() = row2.ItemArray ' Use SequenceEqual to compare arrays. If (array1.SequenceEqual(array2)) Then Console.WriteLine("FIRST ELEMENT OF EQUAL ROWS: " + array1(0).ToString()) End If Next Next End Sub End Module Output FIRST ELEMENT OF EQUAL ROWS: 25

Select.

One Function we can call is Select. This acts like a database query. We pass it is query string and the DataTable itself returns matching rows.DataTable Select

Rows.

This property returns DataRow instances. A DataRow must contain a cell for each DataColumn in the table. We can access fields with the Field extension.DataRowDataRow Field

Columns.

In a DataTable we must have columns. These are a template for the actual data, for the DataRows. Each field's type is specified with a DataColumn.DataColumn

DataSet.

A program can use many DataTables. And with DataSet, we can combine those tables into a single Class—this makes them easier to handle.DataSet

A useful type.

The examples shown here are not useful on their own. But the style of programmatic DataTable mutation is applicable to many programs.

A summary.

DataTable supplies instructions for in-memory data representations. With it (and DataRow and DataColumn) we handle data.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.