Storing and Accessing Data

In this lesson, you will learn about how to use a database to store and access data.

Data is a central concept in computer programming. Most programs use data in one way or another. For example, in an earlier lesson, you entered data in the form of numbers; that data was then used in a calculation and returned in a message box.

In very simple programs, data is represented as fields in the program. However, for more complex programs, data is stored in a structure separate from the program; this structure is called a database.

A database is a collection of data, stored in a file independent of your program. The data stored in a database can be of many different types—text, number, pictures, and more. Different programs can connect to the same database and view and update the data it contains.

A database is typically divided into one or more tables. A table is a collection of related records. For example, if you used a database that held the data for a small business, you might have one table that represented products, another table for orders, and another table for customers.

Database Table Example graphic

Each table is organized into a grid of columns and rows. The columns represent categories for the data in a record, and the rows represent individual records. For example, in the previous illustration, the Orders table contains a separate row or record representing each order, and columns that represent the product ordered, together with the quantity and the price.

To access the data in a database from your program, you first must have a database to access. By using Visual Basic, you can easily create your own database, or use a database that someone else created.

With Visual Basic Express, you can access three different types of databases: Microsoft SQL Server Compact Edition databases, Microsoft SQL Server Express databases, or Microsoft Access databases. In these lessons, you will use a SQL Server Compact Edition database. For more information, see Using SQL Server Compact 3.5 (Visual Studio).

Once you have a database, you connect it to your program by using an object called a DataSet, and then you connect the fields or controls on a form to the data in the database by using a technique called data binding.

When a field in your program, for example a TextBox control, is bound to a column in a database table, you can display the data from that column in the text box, modify the data in the text box and save it in the database, or enter data for a new record and add it to the database.

Although this may sound complex, it really is not difficult. The database tools in Visual Basic make working with data easy, as you will see in the next few lessons.

In this lesson, you learned what a database is and how it relates to your program. In the next lesson, you will learn how to create a database that can be used by your programs.


If you did not install SQL Server 2005 Compact Edition when you installed Visual Basic Express, you will have to install it before continuing on to the next lesson. For more information, see Using SQL Server Compact 3.5 (Visual Studio) and How to: Add a SQL Server Compact 3.5 Database to a Project.

Next Lesson: Creating Your First Database