Contenu connexe
Similaire à Vb.net session 07
Similaire à Vb.net session 07 (20)
Vb.net session 07
- 1. Performing Data Updates
Pre-Assessment Questions
1. Consider the following statements:
• Statement A: Complex data binding is the process of binding a control.
• Statement B: Simple data binding is the process of binding a
component.
Which of the following is correct with respect to the above statements?
a. Both, Statement A and Statement B, are False.
b. Both, Statement A and Statement B, are True.
c. Statement A is True and Statement B is False.
d. Statement A is False and Statement B is True.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 1 of 25
- 2. Performing Data Updates
Pre-Assessment Questions (Contd.)
2. Consider the following statements:
• Statement A: When user click the DataSource property of the DataGrid
control, the drop-down list displays the <dataset.table name> option as
well as the <dataset name> option.
• Statement B: The CurrencyManager class is derived from the
BindingManagerBase class.
Which of the following is correct with respect to the above statements?
a. Both, Statement A and Statement B, are False.
b. Both, Statement A and Statement B, are True.
c. Statement A is True and Statement B is False.
d. Statement A is False and Statement B is True.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 2 of 25
- 3. Performing Data Updates
Pre-Assessment Questions (Contd.)
3. Consider the following statements:
• Statement A: By using parameterized queries, data can be filtered
based on the criterion entered by a user at run time.
• Statement B: Parameterized queries cannot created while configuring
data adapters.
Which of the following is correct with respect to the above statements?
a. Both, Statement A and Statement B, are False.
b. Both, Statement A and Statement B, are True.
c. Statement A is True and Statement B is False.
d. Statement A is False and Statement B is True.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 3 of 25
- 4. Performing Data Updates
Pre-Assessment Questions (Contd.)
4. Consider the following statements:
• Statement A: Data in a dataset can be filtered using the Select()
method.
• Statement B: Select method is a member of DataTable class.
Which of the following is correct with respect to the above statements?
a. Both, Statement A and Statement B, are False.
b. Both, Statement A and Statement B, are True.
c. Statement A is True and Statement B is False.
d. Statement A is False and Statement B is True.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 4 of 25
- 5. Performing Data Updates
Pre-Assessment Questions (Contd.)
5. Consider the following statements:
• Statement A: By default, every table in a dataset has a data view
attached to it.
• Statement B: User cannot create the DataView objects in applications
explicitly.
Which of the following is correct with respect to the above statements?
a. Both, Statement A and Statement B, are False.
b. Both, Statement A and Statement B, are True.
c. Statement A is True and Statement B is False.
d. Statement A is False and Statement B is True.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 5 of 25
- 6. Performing Data Updates
Solutions to Pre-Assessment
Questions
2. a. Both, Statement A and Statement B, are False.
3. b. Both, Statement A and Statement B, are True.
4. c. Statement A is True and Statement B is False.
5. b. Both, Statement A and Statement B, are True.
6. c. Statement A is True and Statement B is False.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 6 of 25
- 7. Performing Data Updates
Objectives
In this lesson, you will learn to:
• Implement cached data updates
• Implement direct data updates
• Maintain data concurrency
• Display data from multiple tables in a single Windows Form
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 7 of 25
- 8. Performing Data Updates
Updating Data
• ADO.NET enables Windows Form to display data from the database.
• Data can be retrieved from database using ADO.NET.
• Database also can be updated with the changes user makes using ADO.NET.
• Data can be retrieved from a database directly through data commands or
cached in datasets.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 8 of 25
- 9. Performing Data Updates
Cached Data Updates
• The sequence of operations that occur during cached data update are:
• Data from the database is cached in a dataset and presented to a user
through an application.
• The user updates the data, that is, adds, deletes, or modifies the data
through the application.
• The dataset is updated with the changes that the user makes through the
application.
• The changes in the dataset are transferred to the database at the back
end.
• Updating the database consists of two steps, updating the dataset and
updating the database.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 9 of 25
- 10. Performing Data Updates
Updating a Dataset
• A dataset can be updated by using their collections, their objects, and their
methods.
• When user make changes to a record in the table, some events are raised by
the DataTable object that are as follows:
• ColumnChanging event
• RowChanging event
• ColumnChanged event
• RowChanged event
• RowDeleting event
• RowDeleted event
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 10 of 25
- 11. Performing Data Updates
Using Events to Validate the Data in a
Dataset
• The ColumnChanging and RowChanging events
• The ColumnChanging event.
• The RowChanging event.
• The Row property.
• Column property
• The Row and Column properties
• The data adapter needs information regarding each change in the dataset to
update the data source.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 11 of 25
- 12. Performing Data Updates
Using Events to Validate the Data in a
Dataset (Contd.)
• Dataset has to be maintain some information while making changes to dataset.
This is as given below:
• The RowState property
• The DataRowVersion enumeration
• Call the AcceptChanges() method of the dataset to accept the changes.
• When the AcceptChanges() method is called, it overwrites the original version
of a record with the current version.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 12 of 25
- 13. Performing Data Updates
Updating a Data Source
• The data source is updated with the changed dataset by calling the Update()
method of the data adapter.
• The data adapter uses the RowState property and the DataRowVersion
enumeration information of the DataRow object to make the changes in the
data source.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 13 of 25
- 14. Performing Data Updates
Direct Data Updates
• Data source also can be accessed directly through data. Data commands are
generally used in the following cases:
• To work with stored procedures that return a result set, which can be
manipulated.
• To access data that is not appropriate for storing in a dataset, such as
data with a short life cycle.
• To access read-only data, that is, data that will not be updated.
• For executing a data command, the Connection, CommandText, and the
Parameters properties for the data command have to be set.
• Connection property is set for specifying the connection.
• CommandText property is set for the SQL query or the stored procedure to be
executed to retrieve the data.
• Parameters property is set for the parameters for the execution of the
command.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 14 of 25
- 15. Performing Data Updates
Demo
Maintaining Data
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 15 of 25
- 16. Performing Data Updates
Problem Statement
• The Diaz Telecommunications Sales Manager needs to view the customer
details in a customized format. In addition, the customer details need to be
maintained, which involves adding new customer details, and modifying and
deleting the existing ones.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 16 of 25
- 17. Performing Data Updates
Solution
• To maintain customer details, you need to perform the following steps:
1. Design a Windows Form to update the data.
2. Connect to the database.
3. Bind the data to the Windows Forms Controls.
4. Write the code to update the data.
5. Perform data maintenance
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 17 of 25
- 18. Performing Data Updates
Maintaining Data Concurrency
• When many people attempt to modify data in a database at the same time, a
system of controls must be implemented so that modifications made by one
person do not adversely affect those of another person, this is called
concurrency control.
• There are three types of concurrency controls:
• Pessimistic concurrency control
• Optimistic concurrency control
• ‘Last in Wins’ concurrency control
• ADO.NET implements optimistic concurrency by using two methods Version
Number Method and Saving All Values Method.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 18 of 25
- 19. Performing Data Updates
Optimistic Concurrency through Data
Adapters
• To ensure that a data adapter, when configured, implements optimistic
concurrency, perform the following steps:
• Drag an OleDbDataAdapter object into the Windows Form.
• Data Adapter Configuration Wizard starts. On the third screen of
the wizard, ensure that the Use SQL statements option is selected.
• On the fourth screen, click the Query Builder button to create a query
for retrieving data. Select a table and its fields to be displayed.
• After the table and its fields are selected, click the OK button.
• Click the Advanced Options button.
• The Use optimistic concurrency check box is selected by default.
• Click the OK button. Then, click the Next button. The SQL statements
for retrieving the data are generated.
• Click the Finish button to create a data adapter.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 19 of 25
- 20. Performing Data Updates
Displaying Data From Multiple Tables
• Data can be displayed from two different tables, using relationship. To
display data from two tables perform the following tasks:
• Design a Windows Form to display data.
• Connect to the database.
• Generate a dataset for the created data adapters.
• Create a relationship between the tables.
• From the Solution Explorer window, select the XML schema for the
dataset that has been created.
• Double-click the schema dsMasterDetails.xsd to view its structure.
The dataset structure is displayed.
• Select View from the menu bar. Then, select Toolbox from the View
menu. The Toolbox is displayed on the left pane of the screen. Select
the XML Schema tab of the Toolbox.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 20 of 25
- 21. Performing Data Updates
Displaying Data From Multiple Tables
(Contd.)
• Drag a Relation control from the XML Schema tab of the Toolbox to an
table of the dataset. The Edit Relation dialog box is displayed. By default,
the name of the relationship is <parent table name child table name>.
• Set the Parent element, Child element, Key Fields, and Foreign Key
Fields values in the Edit Relation dialog box and click the OK button.
• A Relation icon appears between the two tables, depicting a relationship
between them. You can modify the relationship by right-clicking the
Relation icon and choosing the Edit Relation option from the shortcut
menu.
• Bind the data to the Windows Form controls.
• Name the DataGrid control and bind it by setting the DataSource property
and the DataMember property.
• Bind the control on forms and write code to fill dataset on load event of
form.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 21 of 25
- 22. Performing Data Updates
Using Stored Procedures
• Stored procedures are used for encapsulating database operations in a single
command.
• The ADO.NET Command object provides the Parameters collection for calling
a stored procedure.
• To call a stored procedure, you need to pass the stored procedure name
followed by parameter arguments as an SQL statement.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 22 of 25
- 23. Performing Data Updates
Summary
In this lesson, you learned that:
• ADO.NET enables you to display data from the database in a Windows Form.
The sequence of operations that occur during cached data update is:
• The dataset is updated with the changes that the user makes through
the application. At this point, database remains unaffected, as the
changes are made only in the dataset cache.
• The user updates the data, that is, adds, deletes, or modifies the data
through the application.
• The dataset is updated with the changes that the user makes through
the application.
• The changes in the dataset are transferred to the database at the back
end.
• A dataset can be updated by using these collections, their objects, and their
methods.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 23 of 25
- 24. Performing Data Updates
Summary (Contd.)
• The Rows collection of a DataSet class contains rows of data in a DataRow
object.
• The ColumnChanging and RowChanging events raised by the DataTable
object are used to validate the values being entered in the dataset while
updating the dataset.
• The RowState property of the DataRow object indicates the current state of
the record.
• The data source is updated with the changed dataset by calling the Update()
method of the data adapter.
• Data commands can be used in the following cases:
• To work with stored procedures that return a result set
• To access data that is not appropriate for storing in a dataset
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 24 of 25
- 25. Performing Data Updates
Summary (Contd.)
• To access read-only data, that is, data that will not be updated.
• The version number method requires the record to be updated to have a
datetime stamp or a version number column.
• By default, the data adapters that are created in an application implement
optimistic concurrency.
• Tables are stored in a dataset as discrete entities but a relationship can be
created between the stored tables to display data from the related tables.
• You can use stored procedures for encapsulating database operations in a
single command.
• The ADO.NET Command object provides the Parameters collection for calling
a stored procedure.
©NIIT Creating Data Centric Application Using ADO.NET Lesson 2A / Slide 25 of 25