vb .net tutorial - 7

Upload: mathes99994840202

Post on 30-May-2018

246 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 VB .net tutorial - 7

    1/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 1 of 25

    Objectives

    In this lesson, you will learn to:

    Perform cached data updates

    Perform direct data updates

    Identify the need for concurrency management

    Identify the methods of maintaining concurrency in datasets

  • 8/14/2019 VB .net tutorial - 7

    2/25

  • 8/14/2019 VB .net tutorial - 7

    3/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 3 of 25

    Task List

    Identify the data that needs to be maintained.

    Identify the mechanism to maintain data.

    Design a Windows Form to maintain the data.

    Connect to the database.

    Bind the data to the Windows Form controls.

    Write the code to maintain the data.

    Perform data maintenance.

  • 8/14/2019 VB .net tutorial - 7

    4/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 4 of 25

    Task 1: Identify the data that needs to be maintained.

    Result:

    As per the given problem statement, the data to bemaintained is as follows:

    CustID

    FName

    LName

    Address

    Phone

    email

  • 8/14/2019 VB .net tutorial - 7

    5/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 5 of 25

    Task 2: Identify the mechanism to maintain data.

    Data can be retrieved from a database directly throughdata commands or cached in datasets.

    Performing cached data updates Updating the dataset

    While updating the dataset, some events are raised:

    ColumnChangingRowChanging

    ColumnChanged

    RowChanged

    RowDeleting

    RowDeleted

  • 8/14/2019 VB .net tutorial - 7

    6/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 6 of 25

    Task 2: Identify the mechanism to maintain data.(Contd.)

    While updating the dataset, the following information

    is maintained:RowState property

    Unchanged

    Added Deleted

    Detached

    Modified

  • 8/14/2019 VB .net tutorial - 7

    7/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 7 of 25

    Task 2: Identify the mechanism to maintain data.(Contd.)

    DataRowVersion Enumeration

    Original Current

    Proposed

    DefaultThe changes are committed to the dataset by calling

    the AcceptChanges()method.

    Updating the data source

    The data source is updated with the changeddataset by calling the Update()method of the dataadapter.

  • 8/14/2019 VB .net tutorial - 7

    8/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 8 of 25

    Just a Minute

    2. When does the RowState property have the value

    Detached?

    3. What is the relationship between the RowState propertyand the DataRowVersion enumeration?

  • 8/14/2019 VB .net tutorial - 7

    9/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 9 of 25

    Task 2: Identify the mechanism to maintain data.(Contd.)

    Performing direct data updates

    Data commands are can be used to directly update thedata source.

    A data command object can be derived from the

    OleDbCommand class or the SqlCommand class.

    For executing a data command, the Connection,

    CommandText, and the Parameters properties for the

    data command have to be set.

  • 8/14/2019 VB .net tutorial - 7

    10/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 10 of 25

    Task 2: Identify the mechanism to maintain data.(Contd.)

    Result:

    Since you need to access the data through a dataset, youwill perform a cached data update.

  • 8/14/2019 VB .net tutorial - 7

    11/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 11 of 25

    Just a Minute

    1. In which cases, direct data update is preferred to updates

    through datasets?

    2. Write a code to connect to a database through datacommands.

  • 8/14/2019 VB .net tutorial - 7

    12/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 12 of 25

    Task 3: Design a Windows Form to maintain the data.

    Task 4: Connect to the database.

    Task 5: Bind the data to the Windows Form controls.

    Task 6: Write the code to maintain the data.

    Task 7: Perform data maintenance.

  • 8/14/2019 VB .net tutorial - 7

    13/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 13 of 25

    Maintaining Data Concurrency

    Concurrency control is the process of ensuring that in case

    many people attempt to modify data in a database at

    the same time, modifications made by one person do not

    adversely affect those of another person.

    Method Description

    Pessimistic concurrency control In this type of concurrency control, a system oflocks applied does not allow users to modify data

    in a way that affects other users. When a lock is

    applied, other users cannot perform actions that

    would conflict with the lock until the owner

    releases it. Pessimistic control is mainly used in

    environments where the data contention is high

    and protecting data with locks is less costly than

    rolling back transactions in case of concurrencyconflicts. Pessimistic concurrency is not possible in

    a disconnected architecture.

  • 8/14/2019 VB .net tutorial - 7

    14/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 14 of 25

    Maintaining Data Concurrency (Contd.)

    Optimistic concurrency control In this type of concurrency control, the lock is not applied when the users read the data. When the

    user performs a data update, the system checks to

    see if the data has been changed by another user

    after the data was read. If the data has been

    changed, an error is raised. Optimistic concurrency

    is used in environments where the data contention

    is low and protecting data with locks is more costly

    than rolling back transaction in case of concurrencyconflicts.

    Last in Wins concurrency control This type of concurrency control works in a similar

    way as the optimistic concurrency control, with the

    difference that the record is updated regardless of

    whether the record has been changed or not.

    Method Description

  • 8/14/2019 VB .net tutorial - 7

    15/25

  • 8/14/2019 VB .net tutorial - 7

    16/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 16 of 25

    Just a Minute

    What is the difference between the concurrency control

    through the Saving All method and the Version Number

    method?

  • 8/14/2019 VB .net tutorial - 7

    17/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 17 of 25

    Optimistic Concurrency through Data Adapter

    By default, the data adapters that are created in an

    application implement optimistic concurrency.

  • 8/14/2019 VB .net tutorial - 7

    18/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 18 of 25

    Problem Statement 7.P.1

    The Sales Manager of Diaz Telecommunications needs to

    maintain customer details, which involves adding new

    customer details, and modifying and deleting existing

    customer details. While adding and modifying details, the

    application should ensure that no field is left blank.

  • 8/14/2019 VB .net tutorial - 7

    19/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 19 of 25

    Summary

    In this lesson, you learned that:

    Data can be retrieved from a database directly through data

    commands or can be cached in datasets.

    When a dataset is used to access data, updating the

    database consists of two steps, updating the dataset and

    updating the database.

    When you make changes to a record in the table, the

    following events are raised by the DataTable object:

    ColumnChanging

    RowChanging

    ColumnChanged

  • 8/14/2019 VB .net tutorial - 7

    20/25

  • 8/14/2019 VB .net tutorial - 7

    21/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 21 of 25

    Summary (Contd.)

    The data adapter uses the following information while

    updating the data source:

    The RowState property The RowState property of theDataRow object indicates the current state of the

    record.

    TheDataRowVersionenumeration A dataset can

    maintain four versions of a DataRow object: Current,Original, Proposed, and Default.

    After the changes have been made, they are committed to

    the dataset by calling the AcceptChanges() method of the

    dataset to accept the changes made to the dataset.

  • 8/14/2019 VB .net tutorial - 7

    22/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 22 of 25

    Summary (Contd.)

    The data source is updated with the changed dataset by

    calling the Update() method of the OleDbDataAdapter class.

    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 adataset, such as data with a short life cycle

    To access read-only data, that is, data that will not be

    updated

    A data command object can be derived from theOleDbCommand class or the SqlCommand class.

  • 8/14/2019 VB .net tutorial - 7

    23/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 23 of 25

    Summary (Contd.)

    A data command object can be derived from the

    OleDbCommand class or the SqlCommand class.

    For executing a data command, the Connection andthe CommandText properties for the command

    have to be set for specifying the connection and the

    SQL query to be executed to retrieve the data,

    respectively.

    Concurrency means at the same time; thus,

    concurrency management means to decide the action

    that will take place when multiple users try toupdate the same record.

  • 8/14/2019 VB .net tutorial - 7

    24/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 24 of 25

    Summary (Contd.)

    There are three concurrency management methods

    available:

    Pessimistic concurrency control In this type ofconcurrency control, a record is not available to other

    users from the time that a user begins to edit the record

    till the time that the record is updated in the database.

    Optimistic concurrency control In this type ofconcurrency control, the record is not available to other

    users only when the record is being updated in the

    database. When a user tries to update a record that has

    been changed, an error occurs.

  • 8/14/2019 VB .net tutorial - 7

    25/25

    Performing Data Updates

    NIIT Performing Data Updates/Lesson 7/Slide 25 of 25

    Summary (Contd.)

    Last in Wins concurrency control This type of

    concurrency control works similar to the optimistic

    concurrency control, the only difference being that the

    record is updated without checking whether the recordhas been changed or not.

    When the row is changed and there is an attempt to

    update the database, ADO.NET uses two methods to

    determine if any changes have occurred:

    The Version number method

    The saving all values method

    By default, the data adapters that are created in an

    application implement optimistic concurrency.