Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

SUMMARY 961

While SQLReader.Read

txtOrders.Text = SQLReader.Item(“Total Orders”).ToString

txtAmount.Text = SQLReader.Item(“Total Amount”).ToString

End While

SqlConnection1.Close()

End Sub

Summary

In this chapter, you’ve learned about the basic objects of ADO.NET through examples. You know how to connect to a database, set up DataAdapter objects for each table you want to query, and populate a DataSet object with one or more tables. The tables in a DataSet are usually related, but the relations between tables are not copied from the database automatically. You must establish relations between them by editing the XSD file with the schema of the DataSet

Once the data is in the DataSet, you can process it in any way you like. In this chapter, you learned how to bind controls to the fields of the tables in the DataSet, so that users can navigate through the rows of the tables and even edit them. The DataGrid control is a very flexible tool for presenting multiple related tables, and you know how to use it in your applications. You also know how to use the ListBox and ComboBox controls as lookup devices by binding them to a field in a table.

In the following chapter, you’ll learn how to access the contents of a DataSet programmatically, as well as how to update the database. You will also learn how to create DataSet objects in your code and how to save them to disk files. This technique allows you to create custom data stores or exchange data with other systems.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 22

Programming the

ADO.NET Objects

In the preceding chapter, you learned how to build applications that access databases with point and click operations. Binding fields to controls isn’t your only option; it isn’t event the best option. As you recall, the Update method can’t handle update errors. The DataGrid control isn’t quite appropriate for editing rows either. It’s a great tool for displaying data, but it’s not the most appropriate control for editing rows. A major limitation of the DataGrid control is that it can’t display lookup fields. It can handle related rows, but at any given time it can only display the rows of a single table—you can’t use the DataGrid control to display master/detail forms. To keep a customer’s data, the customer’s orders, and an order’s details all visible on the form, we had to use multiple DataGrid controls on our form.

Database applications aren’t trivial, and the process of building database applications can’t be oversimplified. Throwing a few controls on the form and setting their data-binding properties works for very simple applications, but for everything else you must provide quite a bit of code. You must validate your data and minimize the amount of data moved back and forth between the client and the server. The DataSet object’s Update method passes all the rows it contains to the database and uses the SQL statements generated by the configuration wizard to update the underlying tables. In most cases, very few of the rows have been edited, and you should be able to transmit only the rows that were added, deleted, or modified.

Another important consideration is how the DataSet is populated. Do we move all the data we may need in the course of the application? This is what the DataSet object does best. It maintains a copy of selected tables in the client computer’s memory, so that the application won’t have to hit the server for every row the user may need to view. The more data you move into the DataSet, the longer the user can work with the disconnected data. There are applications, however, where this isn’t desirable. An invoicing application can’t keep the price list in memory for too long, because prices may change in the database. It should also be able to update the current stock.

To build reliable, robust database applications, you must learn to program the objects exposed by ADO.NET. You have seen how to use the Command and DataReader objects in the previous chapter. In this chapter, you’ll learn how to access the data stored in the DataSet from within your code and how to execute commands directly against the database. We’ll also explore the DataForm wizard, a tool for building simple data-browsing and -editing applications.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com