How to Use Data Sources with Databases - How to create a connection to a database
(Page 4 of 5 )
After you choose the data source type, the Data Source Configuration Wizard displays a dialog box that lets you choose the data connection you want to use. From this dialog box, you can select an existing connection (one you’ve previously defined), or you can click the New Connection button to display the Add Connection dialog box. This dialog box helps you identify the database that you want to access and provides the information you need to access it. That includes specifying the name of the server that contains the database, entering the information that’s required to log on to the server, and specifying the name of the database. How you do that, though, varies depending on whether you’re running SQL Server Express on your own PC or whether you’re using a database server that’s running on a network server.
If you’re using SQL Server Express on your own PC and you’ve downloaded and installed it as described in appendix A, you can use the localhost keyword to specify that the database server is running on the same PC as the application. This keyword should be followed by a backslash and the name of the database server: SqlExpress.
For the logon information, you should select the Use Windows Authentication option. Then, SQL Server Express will use the login name and password that you use to log in to Windows as the name and password for the database server too. As a result, you won’t need to provide a separate user name and password in this dialog box.
Last, you enter or select the name of the database that you want to connect to. In this figure, for example, the connection is for the MMABooks database that’s used throughout the chapters in this section of the book. When you’re done supplying the information for the connection, you can click the Test Connection button to be sure that the connection works.
In contrast, if you need to connect to a database that’s running on a database server that’s available through a network, you need to get the connection information from the network or database administrator. This information will include the name of the database server, logon information, and the name of the database. Once you establish a connection to the database, you can use that connection for all of the other applications that use that database.
By default, Visual Studio assumes you want to access a SQL Server database as shown here. This works for SQL Server 7, 2000, and 2005 databases including SQL Server Express databases. If you want to access a different type of database, though, you can click the Change button to display the Change Data Source dialog box. Then, you can select the data source and the data provider you want to use to access that data source. If you want to access an Oracle database, for example, you can select the Oracle Database item in the Data Source list. Then, you can choose the data provider for Oracle or the data provider for OLE DB from the Data Provider drop-down list.
The Add Connection and Change Data Source dialog boxes Description

Description
- By default, a connection uses the SQL Server data provider. If that isn’t what you want, you can click the Change button in the Add Connection dialog box to display the Change Data Source dialog box. Then, you can choose the right data source and data provider.
- To be sure that the connection is configured properly, you can click the Test Connection button in the Add Connection dialog box.
- The next dialog box (not shown) asks if you want to save the connection string to the application configuration file (app.config), and we recommend that you do that. Then, if the connection string changes later on, you can change the string in the app.config file rather than in each form that uses the connection string.
Express Edition differences
- The Change Data Source dialog box provides only two options: Microsoft Access Database File and Microsoft SQL Server Database File.
- The Add Connection dialog box is simpler, and it includes a Database File Name text box that you use to specify the database. To do that, you click the Browse button to the right of the text box and use the resulting dialog box to point to the data file for the database.
Figure 14-3. How to create a connection to a database
Next: How to choose database objects for a data source >>
More Visual Basic.NET Articles
More By Murach Publishing
|
This article is excerpted from chapter 14 of the book Murach's Visual Basic 2005, written by Anne Boehm (Murach, 2006; ISBN: 1890774383). Check it out today at your favorite bookstore. Buy this book now.
|
|