1. Trang chủ
  2. » Công Nghệ Thông Tin

Particular Statement Block pps

5 202 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 24,42 KB

Nội dung

Using a finally Block It is important to remember that when an exception is thrown, it changes the flow of execution through the program. This means you can't guarantee that a statement will always run when the previous statement finishes, because the previous statement might throw an exception. Look at the following example. It's very easy to assume the call to reader.Close will always occur. After all, it's right there in the code: TextReader reader = src.OpenText(); string line; while ((line = reader.ReadLine()) != null) { source.Text += line + "\n"; } reader.Close(); Sometimes it's not an issue if one particular statement does not run, but on many occassions it can be a big problem. If the statement releases a resource that was acquired in a previous statement, then failing to execute this statement results in the resource being retained. This example is just such a case: If the call to src.OpenText succeeds, then it acquires a resource (a file handle) and you must ensure that you call reader.Close to release the resource. If you don't, sooner or later you'll run out of file handles and be unable to open more files (if you find file handles too trivial, think of database connections instead). The way to ensure a statement is always run, whether or not an exception has been thrown, is to write that statement inside a finally block. A finally block occurs immediately after a try block, or immediately after the last catch handler after a try block. As long as the program enters the try block associated with a finally block, the finally block will always be run, even if an exception occurs. If an exception is thrown and caught locally, the exception handler executes first, followed by the finally block. If the exception is not caught locally (the common language runtime has to search through the list of calling methods to find a handler), the finally block runs first. In any case, the finally block always executes. The solution to the reader.Close problem is as follows: TextReader reader = null; try { reader = src.OpenText(); string line; while ((line = reader.ReadLine()) != null) { source.Text += line + "\n"; } } finally { if (reader != null) { reader.Close(); } } Even if an exception is thrown, the finally block ensures that the reader.Close statement always executes. You'll see another way to solve this problem in Chapter 13, “Using Garbage Collection and Resource Management.” • If you want to continue to the next chapter Keep Visual Studio 2005 running and turn to Chapter 7. • If you want to exit Visual Studio 2005 now On the File menu, click Exit. If you see a Save dialog box, click Yes. 1. On the Data menu, click Show Data Sources. The Data Sources window appears, displaying NorthwindDataSet with the Products and Suppliers DataTable objects. Expand the Products and Suppliers DataTable objects. The columns in each table appear, together with an icon indicating how each column will be displayed on the form. Most columns will be displayed as textbox controls, although the Discontinued column in the Products DataTable will appear as a checkbox; this is because the corresponding column in the database is a bit column that can only contain True/False values. Also notice that Products appears twice: once as a DataTable object in its own right, and once as a column in the Suppliers DataTable. You will shortly see how this feature allows the relationship between a supplier and the products it supplies to be coordinated. 2. Click the Suppliers DataTable. A drop-down menu appears by the name. Click the drop-down menu, and select Details. This action will change the display layout for suppliers to a set of fields rather than the default grid layout. The Details layout is useful for displaying the data in the “one” side of a many-to-one relationship, while the Grid view is more suited to the “many” side. 3. Click the SupplierID column in the Suppliers DataTable. Another drop-down menu appears. Click this drop-down menu. You will see the different ways that the data in this column can be presented. The SupplierID column is actually the primary key for this table in the database, and so it should not be changed. For this reason, click the Label control. 4. Click the Suppliers DataTable and drag it to the top left corner of the Suppliers and Products form. Notice that a number of components appear underneath the form. The table below summarizes these components. Also notice the tool strip that appears at the top of the form. This tool strip contains items for navigating through the list of suppliers, as well as for adding, modifying, and deleting rows, and for saving changes back to the database. Component Description northwindDataSet This is the data source used by the form. It is a NorthwindDataSet object. It provides methods for updating data in the database. suppliersBindingSource This component acts as an intermediary between the controls on the form, and the data source. A BindingSource component keeps track of the currently selected row in the DataSet, and ensures that the controls on the form display the data for that row. A BindingSource provides methods for navigating through a DataSet, adding, removing, and updating rows suppliersTableAdapter This is the TableAdapter object for the Suppliers table, providing methods for retrieving rows from the Suppliers table in the database and populating the data source. suppliersBindingNavigator This is a BindingNavigator control that provides a Component Description standardized mechanism for navigating through the rows in a DataSet. It is the visible tool strip that appears at the top of the form containing the tool strip items for most of the common data-related actions. TIP If the fields are displayed too high up on the form and encroach on the tool strip, while they are still selected, simply drag the Supplier ID field to the appropriate location. The remaining fields will also move. 5. Click the Products DataTable that is nested inside the Suppliers DataTable and drag it onto the form, to the right of the Supplier fields. A DataGridView control appears on the form. Also notice that two more components appear underneath the form: productsBindingSource which is a BindingSource control that coordinates the rows in the DataGridView with northwindDataSet, and products TableAdapter which is used to retrieve rows from the database into the Products Data Table. TIP Be sure to drag the Products DataTable that is nested inside the Suppliers DataTable rather than the top-level Products DataTable in the NorthwindDataSet. If you use the top-level Products DataTable, the display will not be coordinated properly at runtime; all products will always be displayed rather than those supplied by the displayed supplier. 6. Click the DataGridView control on the form and expand it to fill the right-hand side of the form. 7. While the DataGridView control is still selected (click it if it is not), click the Smart Tag handle that appears at the top right-hand corner of the control. The DataGridView Tasks dialog box appears. You can use this dialog box to quickly modify the commonly used properties of the DataViewGrid control, and perform tasks such as changing the properties of the columns displayed, and changing the actions supported by the control. To keep the application straightforward, clear the Enable Adding, Enable Editing, Enable Deleting, and Enable Column Reordering checkboxes. You will learn more about using the DataGridView control in Chapter 24, “Working with Data Binding and DataSets.” 8. Start the application running without debugging. When the form appears, the first supplier (Exotic Liquids) is displayed together with the three products it supplies. Click the Move next button in the tool strip. The supplier New Orleans Cajun Delights appears, together with the four products it supplies. You can overtype any of the supplier's details to change them, although the changes will not be saved to the database until you click the Save button in the tool strip. 9. Click the Add new button in the toolstrip. The form clears, and you can enter the details of the new supplier. Notice how a new supplier ID is automatically generated. As before, the details of the new supplier will not be saved to the database unless you click the Save button in the tool strip. 10. Click the Delete button in the tool strip to remove this new supplier. Existing supplier number 29 automatically appears. 11. Close the form without saving any changes to the data and return to the Visual Studio 2005 programming environment. . to ensure a statement is always run, whether or not an exception has been thrown, is to write that statement inside a finally block. A finally block occurs immediately after a try block, or immediately. not an issue if one particular statement does not run, but on many occassions it can be a big problem. If the statement releases a resource that was acquired in a previous statement, then failing. immediately after the last catch handler after a try block. As long as the program enters the try block associated with a finally block, the finally block will always be run, even if an exception

Ngày đăng: 01/07/2014, 09:20

TỪ KHÓA LIÊN QUAN

w