Figure 53 Choose a Data Source Type Step

[View full size imaqel

frata Source Configuration Wizard

EE

Choose a Data Source type

1 VVh«rf will the appliutior get data fnun?

DdtobaK web Servie Gt?]«t

.

Lets yw corrí;t to ^ c^iraí^se 3rd çhoose tr¡e d^tabwf íbjííts y eux Th:í; üpt^n çr^^S (iatûsci.

V IO'IjS

rjext > ; Caneé

Let's start with databases, since they are easier to understand if you have worked with data binding in the past in .NET.

4 PREY

NEXT +

4 PFtEV

NEXT

Adding a Database Data Source

When you select Database for the data source type, what you really are doing is adding a typed data set definition to your project that is ready to contain data from the database objects that you select in the wizard. You could accomplish the same thing by selecting a data set from the Add New Item dialog, then dragging and dropping database objects on the designer surface from Server Explorer. The Data Source Configuration wizard steps you through the process in a little different way. The following procedure outlines this process.

1. Choose your database connection (see Figure 5.4).

Figure 5.4. Choose Your Database Connection Step

[View full size imagel

Figure 5.4. Choose Your Database Connection Step

[View full size imagel

The drop-down list shows the data connections that have been configured in Visual Studio through this wizard or through the Server Explorer window. These same items appear in the Data Connections node in the Server Explorer tree.

Depending on the options selected when the connection was created, the section below the drop-down list may indicate that the connection string contains sensitive information (specifically, a username and password). If so, it gives you the option to leave that information out of the connection string so that the sensitive information doesn't get embedded in your configuration file. You can also view the resulting connection string by clicking on the plus sign next to the Connection String group header. This displays the connection string that will be used in a selectable text box. You can select the string (in case you need to copy it to the Clipboard), but you cannot edit the string directly.

2. If you click the New Connection button, what you see depends on whether you have configured any connections before. The first time you add a connection, you are prompted to select a data source and provider (see Figure 5.5). You also get the same dialog if you click the Change button from the Add Connection dialog (seeFigure 5.6). The Add Connection dialog lets you create a new connection based on any of the available providers, and once you do, it too will be added to the list of data connections in Server Explorer.

Figure 5.5. Data Source and Provider Selection

Figure 5.5. Data Source and Provider Selection

Figure 5.6. Add Connection Dialog

Add Connection ? X

Enter information to connect to the selected data source or dick ""Change1 to choose a different data source and/or provider.

Data source:

Microsoft, SQL Server (SqlClient)

Change...

Server name:

localhost

v

Refresh

Log on to the server

{+) Use Windows Authentication

The Add Connection dialog is new in Visual Studio 2005, but it's very similar to the one that existed in previous versions. The items presented in this dialog change based on the provider selected. Figure 5.6 shows the settings for the managed SQL Server provider. To configure a SQL Server connection, you specify a server namethis can be a SQL Server instance name on the network or an IP address. If you are referring to the local machine's default instance of SQL Server, you can use one of three shorthand addresses: localhost, (local), or just the dot (.) character. You also provide authentication information and specify the database name. This configures the connection string that is used to connect to the database.

If you had selected a SQL Server database file as the data source ¡Figure 5.5, the dialog would be different and would only let you specify a path to the database file and authentication information. This is the way to specify a data connection for a SQL Server 2005 Express Edition database.

3. Choose whether to save the connection information in your application configuration file (Figure 5.7). Doing so lets you easily modify the connection string when you deploy your application without needing to change any of the source code.

Figure 5.7. Save Connection String Step

Visual Studio adds code to the table adapter definitions to read in the connection string from the .config file if it can be found; otherwise, the table adapter tries to use a hard-coded default (the one specified at design time through this process).

In the next window you select the objects from the database that you want to include in the typed data set that will be generated as the output of this wizard process (see Figure 5.8). As mentioned earlier in the book, you can include tables, views, stored procedures, or functions in the data set simply by checking the boxes next to them in the tree of database objects. This is analogous to dragging these objects onto the data set designer surface from Server Explorer. At the bottom of the dialog you can specify the type name for the generated typed data set class.

Figure 5.8. Choose Database Objects Step

When you click the Finish button, the typed data set definition will be added to your project as an XML Schema Definition (XSD) file with an associated typed data set definition source file that is hidden by default. You can view the actual typed data set code by expanding the Solution Explorer tree underneath the XSD file. Underneath the XSD file is a .Designer.cs file for the data set that contains the autogenerated class definitions for the data set and its associated table adapters. Additionally, the Data Sources window will update to show the objects in your data set.

4 PREV

NEXT +<

0 0

Post a comment