DataSet in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the DataSet control


The DataSet is a complex and very powerful object that is actually an equivalent to "in-memory representation" of a database.

It can contain DataTables that correlate to database tables while DataTables can contain a series of DataColumns that define the composition of each DataRow.
As you guess, the DataRow correlates to a row in a database table.

In addition you can establish relationships between DataTables within the DataSet in the same way that a database has relationships between tables.

However, be aware that a raw DataSet is weakly typed which is the biggest issue with it.
Well of course the type of each column can be queried prior accessing of the data elements, but that could add overhead and can make the code very unreadable.

That's where the Strongly typed DataSets jump in.
They combine the advantages of a DataSet with strong typing (in other words, creating strongly typed properties for all database fields) to ensure that data is accessed correctly at design time.

When you drag and drop a DataSet to the form it will be named "DataSet1" by default and will appear in the bottom grey area of the designer (just as it's shown on the image below):


Then in the properties window you may add one or more tables using its Tables collection property:

tables property

Then a new tables collection editor appears and allows you to add new remove DataTable objects to or from an untyped dataset.

tables collection

Voila, your DataSet is ready to use.

Note: the very same could be done programmatically e.g.

    ' instantiate DataTable object
    Dim table As New DataTable("Students")
    table.Columns.Add("ID", Type.GetType("System.Int32"))
    table.Columns.Add("FirstName", Type.GetType("System.String"))
    table.Columns.Add("LastName", Type.GetType("System.String"))
    table.Columns.Add("Phone", Type.GetType("System.String"))

    table.Rows.Add(1, "Asher", "Foster", "(174) 396-9405")
    table.Rows.Add(2, "Brody", "Cabrera", "(579) 668-1752")
    table.Rows.Add(3, "Addison", "Keith", "(369) 377-6950")
    table.Rows.Add(4, "Jin", "Guerra", "(897) 713-7985")

    ' instantiate DataSet object
    Dim studentSet As New DataSet("University")
    ' add the DataTable we created previously

    ' display the first name of the very first student

Previous Lesson | Next Lesson

Share This

Home | About | Contact | Privacy Policy

Copyright 2017 - All Rights Reserved.