Getting the Information You Need: Connecting to an Existing Database

In this lesson, you will learn how to connect your program to an existing database.

Connecting to an existing database is easy. You can use the visual tools in Visual Basic Express to browse to a database and add a local copy of the database to your project. In this lesson, you will create a new project and connect it to the Addresses database that you created in the previous lesson.

To connect to an existing database

  1. On the File menu, click New Project.


    Complete Creating Your First Database before you continue.

  2. In the New Project dialog box, in the Templates pane, click Windows Form Application.

  3. In the Name box, type Addresses and then click OK.

    A new Windows Forms project opens.

  4. On the Data menu, click Show Data Sources.

  5. In the Data Sources window, click Add New Data Source.

    The Data Source Configuration Wizard starts.

  6. Select Database and click Next.

  7. Click New Connection.

    The Add Connection dialog box opens.

  8. In the Add Connection dialog box, check whether the Data source is Microsoft SQL Server Compact 3.5 (.NET Framework Data Provider for Microsoft SQL Server Compact 3.5). If it isn't, click Change, select Microsoft SQL Server Compact 3.5 in the Change Data Source dialog box, and then click OK.

  9. Click the Browse button, navigate to the location where you saved the FirstDatabase.sdf database, click the database file, and then click Open.

  10. Click OK to close the dialog box, and then, in the Data Source Configuration Wizard, click Next. If prompted to copy the data file to your project, click Yes.

  11. On the next page of the wizard, the Yes, save the connection as check box should be selected. Click Next to continue.

  12. On the Choose Your Database Objects page, expand the Tables node, and then select the check box for the Addresses table.

  13. Click Finish.

    A local database file is added to your project. Notice that a FirstDatabaseDataSet object appears in the Data Sources window.

  14. In Solution Explorer, select the FirstDatabase.sdf node.

  15. In the Properties window, make sure that the value of the Copy to Output Directory property is set to Copy if newer.

  16. On the File menu, click Save All to save your project.

In this lesson, you learned how to add a local database to your project. In the next lesson, you will learn how to create a user interface to display the data in the database.

Next Lesson: Showing Information to the User: Displaying Data in Your User Interface