Array Class Collections File String Console Data Directive Enum Exception If Interface Lambda Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows
Data is often stored in tables. And these tables often reside in databases. In the .NET Framework, the DataTable type stores data in memory. It is used often in VB.NET programs. It has Columns and Rows properties.
Note:DataTable is an in-memory representation of structured data—such as that read from a database.
This program uses a DataTable type instance. It defines two routines: Main, and GetTable, which returns a new DataTable. When the GetTable function is invoked, it creates a new DataTable and adds four columns to it.
Columns are named with a string argument and a Type argument. They are of type Integer, String, String, and DateTime. In a DataTable, each column allows a specific type of data. The GetTable method adds five rows to the DataTable.
Tip:The arguments to the Rows.Add method are of the types specified in the columns.
Program that creates DataTable: VB.NET 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
This program alone is not useful for creating a functional data-driven application. But 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, or displaying them on a DataGridView control in Windows Forms. DataTable is often used together with other System.Data types.
Advanced functionality is available on the DataTable. 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
Programs often process data stored in databases. You can use many different types in the .NET Framework to accomplish this task. The DataGridView is a popular choice. It works well with the DataTable.DataColumnDataSetDataGridView
Row:A DataRow must contain a cell for each DataColumn in the table. We can access fields with the Field extension.DataRowDataRow Field
We explored the DataTable type. The example shown here is not useful on its own. But this style of programmatic DataTable mutation is applicable to many VB.NET programs. This includes ASP.NET web applications.
Review:DataTable describes the instructions that form an in-memory data representation object.