Adding a Data Source to an Application


Go back to the design view for Form1 in the GettingData project. Change the form's name to GettingData, then go to the Data menu and choose Add New Data Source... as shown in Figure 25-10.

image from book
Figure 25-10

This brings up the Data Source Configuration Wizard.

The Data Source Configuration Wizard

Adding a data source brings up the Data Source Configuration Wizard, shown in Figure 25-11.

image from book
Figure 25-11

A data source is a generic connection to a database or other source of data for your application. The data source can be a database, a file, a Web service, or a generic object, which is why pressing Next brings you to the Choose Data Source Type dialog, shown in Figure 25-12.

image from book
Figure 25-12

The default choice, Database, is the type of data source that you want, so simply press Next. The next screen, shown in Figure 25-13, lets you choose the data connection you want.

image from book
Figure 25-13

Since you have only one connection configured (the one you set up earlier to access the Northwind database), this is the only one available in the list. If you had not built one yet, you could also select New Connection... and add it here. Click on this connection in the drop-down list. Note that it creates the Connection string for you, based on the choices you made in setting up the connection. You saw connection strings in Chapter 24; this dialog makes it very easy to manage them. Click Next to move on to the next screen (see Figure 25-14), where you are prompted to save the connection in the application configuration file.

image from book
Figure 25-14

Saving the connection in the configuration file (already checked as this is the default) is a good idea, because it can be changed in the configuration file at installation time without having to rebuild the application code. While all applications can benefit from configuration options, database apps are particularly notorious for always having some exception to the standard configuration when deployed to the field, such as a different server name or different network setup. Next, specify what data from your database is loaded into your application in the Choose Your Database Objects dialog, shown in Figure 25-15.

image from book
Figure 25-15

These are the database objects you saw in the Server Explorer earlier; this wizard lets you do some exploration to choose the objects of interest. To begin this process, expand the Tables node to show the tables in the Northwind database, as shown in Figure 25-16.

image from book
Figure 25-16

You see the Customers table as before. Expand the Customers node to see the individual data columns. Check the boxes for the first three, CustomerID, CompanyName, and ContactName. These are what you will display on your form. While you could pick all the fields, that would make for a crowded form to say the least; these will be enough! Now click the Finish button to finish creating the data source.

Now, to use the data source you just created, go back to the Data menu and choose Show Data Sources, so the Data Source window will be displayed in your project, as shown in Figure 25-17.

image from book
Figure 25-17

Adding Data-Bound Controls to a Form

Now your newly added data source appears in the Data Sources window. Expand the Customers node so that the three columns you selected appear. Click on the first one, CustomerID, to select it. Then, drag the CustomerID control onto the GettingData form, as shown in Figure 25-18.

image from book
Figure 25-18

This will create a data-bound CustomerID text box with a label on your form, and several nongraphic data controls are added as well, at the bottom, as shown in Figure 25-19.

image from book
Figure 25-19

What are these strange pseudocontrols at the bottom of your form, northwindDataSet, customersBindingSource, customersTableAdapter, and customersBindingNavigator? They do not seem to appear on the screen if you run the application.

Actually, one of the new objects does appear on the form. This is the customersBindingNavigator, a navigation bar at the top of your form for moving through the rows in the database, as shown in Figure 25-20.

image from book Figure 25-20

It has buttons to move to the first row in the table, the previous row, the next row, the last row, and so on. You look at this control in just a bit.

VS2005-Generated ADO.NET Objects

The other objects you see at the bottom of your form design window are not actually visible on the form, but are wizard-created ADO.NET objects, automatically added to your project by VS2005.

The northwindDataSet object is a strongly typed DataSet. As you learned in Chapter 24, the DataSet is one of the primary ADO.NET classes, allowing you to bring in data from various data sources, store it in application memory, and manipulate and convert it to various formats.

What you may not have noticed is the addition to the Solution Explorer. If you look, a new file has been added to your project, named NorthwindDataSet.xsd, as shown in Figure 25-21.

image from book Figure 25-21

This file is the schema for the northwindDataSet. The XSD (XML Schema Definition) schema is a document by which you can verify the structure of an XML document. XML, a text format used to represent data, has become particularly important with the growth of the Internet. The XSD schema is an important tool for ADO.NET because a DataSet object uses XML under the covers to organize and structure data. Automatically generated by Visual Studio, the schema file specifies the structure of the DataSet, each table, and all relationships between tables. XML and XSD schemas are described more fully in Chapter 23 of this book, but what is important to know for developing data applications is Visual Studio 2005 uses XML to represent the structure of a DataSet, and luckily VS2005 can generate this representation for you!

Two other ADO.NET objects added to your project connect the DataSet to the database; these are the customersTableAdapter and customersBindingSource. The table adapter contains the basic information for the data that you can expect to return from the database. Think of the adapter as the bridge between the DataSet and the database. The table adapter is a single-table version of the more general DataAdapter object, which you worked with in the previous chapter as one of the components of a .NET data provider. It is used to fill a DataSet with data from the database and to send changes made in the DataSet back to the database. the customersDataConnection object contains all the information that ADO.NET needs to connect to your database.

However, you don't need to know the details, you have a running data-based application already as you shall see! Use the following Try It Out to run the application you have created.

Try It Out – Compile and Run the Database Application

image from book

Follow these steps to run the GettingData program:

  1. Just to make the application look more interesting when it runs, drag the other two columns from the data source (CompanyName and ContactName) onto your form. You might play around with the alignment of the forms and labels to make them look neat and clean.

  2. Press F5 to build the GettingData project and run it in the debugger, and you will see your form contains the data from the first row of the Customers table in the Northwind database, as shown in Figure 25-22.

    image from book Figure 25-22

  3. There are indeed 91 customers in the Northwind database, as shown in the customersBindingNavigator bar at the top of the form. Note the full set of navigational buttons. Press the left-arrow button a few times to go forward through a few records, then press the left-arrow+bar (End) button to go to the last record, as shown in Figure 25-23.

    image from book
    Figure 25-23

    Cool! And you didn't have to write any of the code that does the heavy lifting!

  4. Press the X (Close) button in the upper-right corner to stop the application, or press Shift+F5 to stop debugger.

image from book

Of course, there is still a fair amount of work involved. You had to drag the individual data fields from the data source three times, and then the user has to click on the navigator buttons a bunch of times to get to the record of interest; can you make it any easier? You can indeed with a DataGridView control!




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net