RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


.NET Basics for the Database Developer: Take the Plunge : Page 2

This short introduction to .NET shows Access and SQL Server developers how to use a development tool to create connections to data and a user interface to manipulate that data.


Connecting To and Retrieving Data

If you're coming from Access or SQL Server, you'll appreciate VB Express' connection wizard because it performs all the grunt work for you. The following quick example walks through connecting to and retrieving data from Northwind (a sample database that comes with Access):

    Creating a New Project

  1. Launch VB Express and click New Project on the Standard toolbar to start a new project. Click Windows Form Application in the resulting dialog box.
  2. Enter a meaningful name in the Name control, as shown in Figure 1. Using this method, VB Express creates a new directory off the default directory using the project's name. If you want to save the project in a directory other than the default directory, repeat Step 1 but click Save All instead of New Project.
    Figure 1. Give Your Project a Meaningful Name: Enter a meaningful name in the Name control.
  3. Click OK. The Solution Explorer to the right contains files. By default, each new project contains a "My Project" item and a form, named Form1. MS Access developers might be tempted to relate VB Express components in the IDE to Access .mdb files, but that really isn't an accurate comparison. You can think of .NET as an .mdb file without walls or boundaries, but you can reuse a project in another solution by just adding it to the new project. You can't do that with .mdb files.

    Connecting to the Database

  4. To make a connection to the MS Access Northwind database, click the Data Sources tab (bottom right) in Solution Explorer. If the tab isn't available, choose Show Data Source from the Data menu.
  5. Click Add New Data Source to launch the Data Source Configuration Wizard.
  6. Click Next as the Database option is the default.
  7. Click New Connection in the next pane.
  8. In the resulting Add Connection dialog box, click Change and choose Microsoft Access Database File from the Change Data Source dialog box (see Figure 2). Then click OK.
  9. Click Browse in the Add Connection dialog box and locate Northwind.mdb (in the Samples folder of your Office directory) as shown in Figure 3. If the database is password protected, enter the password. Northwind.mdb isn't password protected, so you don't need to enter anything this time. Click OK.

    Figure 2. Specify the Database Type: Click Change, and choose Microsoft Access Database File from the Change Data Source dialog box.
    Figure 3. Identify the Database: Click Browse in the Add Connection dialog box and locate Northwind.mdb.

  10. Click Test Connection and then click OK to clear the confirmation message.
  11. Figure 4. VB Express Asks You If You Want to Copy the Database: Decide whether to copy the entire database or just the data you need.
  12. With the connection working, click OK to return to the wizard, and click Next to continue.

    Retrieving the Data

  13. At this point, the wizard needs you to identify the data. VB Express asks you if you want to copy the database (see Figure 4). If you click Yes, it will copy the entire database. Only click Yes if you truly mean to copy the entire database into your project. This time, click No.
  14. The next pane lets you save the connection string in the configuration file. The default is Yes, and most of the time, this is the best choice. Click Next.
  15. The wizard will retrieve the data objects in the connected database and display them in the source file. This is where you identify the data you want to retrieve and manipulate in your .NET project. Click the plus sign (+) to expand the Tables node. Next, check the Products table, as shown in Figure 5. Notice that VB Express supplies a default name for the dataset. Most of the time, this name is adequate, but you can change it.
  16. Click Finish and VB Express will retrieve the specified data. Figure 6 shows the NorthwindDataSet in the example project.

    Figure 5. The Products Table: Identify the tables and views you want to include in your .NET project.
    Figure 6. The Wizard Connected to Northwind's Products Table: Here is the NorthwindDataSet in the example project.

  17. Choose Save All from the File menu and then click Save. If you didn't specify a specific directory earlier, you can do so now. Most of the time, the default directory is adequate during development.

Click the Solutions Explorer tab to see the changes (see Figure 7). The Solutions Explorer contains two new files: app.config and NorthwindDataSet.xsd. Simply double-click them to see the actual files. Figure 8 shows app.config with the connection string highlighted. This file contains the code the wizard created. Figure 9 shows the graphic representation of the data source.

Figure 7. The Solutions Explorer Tab: After connecting to Northwind.mdb, the Solutions Explorer contains two new files.
Figure 8. App.config with the Connection String Highlighted: This code connects to the data source, Northwind.mdb.
Figure 9. A Familiar Representation of the Data Source: VB Express shows a graphic representation of the data source.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date