ado.net session05

15
Slide 1 of 15 Ver. 1.0 Developing Database Applications Using ADO.NET and XML Session 5 In this session, you will learn to: Working in a connected environment Objectives

Upload: niit-care

Post on 16-Apr-2017

1.772 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Ado.net session05

Slide 1 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

In this session, you will learn to:Working in a connected environment

Objectives

Page 2: Ado.net session05

Slide 2 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

A data adapter is integral to the working of ADO.NET because data is transferred to and from a database through a data adapter. A data adapter retrieves data from a database into a dataset. The data adapter first compares the data in the dataset with that in the database and then updates the database.Data from a database can be accessed by configuring a data adapter.

Working with Data Adapters

Page 3: Ado.net session05

Slide 3 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

Following are the data adapters that can be configured to connect to a database:

SqlDataAdapter

OleDbDataAdapter

OdbcDataAdapter

OracleDataAdapter

Working with Data Adapters (Contd.)

Accesses data specifically from Microsoft SQL Server

Accesses data from a database that is supported by an OLE DB data provider

Accesses data from a database that is supported by an ODBC data provider

Accesses data from a database that is supported by an Oracle data provider

Page 4: Ado.net session05

Slide 4 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

The following properties and methods of a data adapter can be used to perform various operations on a database:

SelectCommand

InsertCommand

UpdateCommand

DeleteCommand

Fill()

Update()

Working with Data Adapters (Contd.)

Refers to a DML statement or a stored procedure to retrieve data from a database

Refers to a data command to insert data into a database

Refers to a data command to update a database

Refers to a data command to delete data from a database

Fills the dataset with the records from a database

Executes the corresponding Insert, Update, or Delete commands for each inserted, modified, or deleted row to reflect the changes in a database

Page 5: Ado.net session05

Slide 5 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

Consider the following code snippet of creating a DataAdapter object and using the SelectCommand property of the object:SqlConnection cn = new SqlConnection();cn.ConnectionString = "Data source=SQLSERVER01;Initial catalog=HR;User id=sa;Password=niit#1234";DataSet DataSet1 = new DataSet();SqlDataAdapter da = new SqlDataAdapter();SqlCommand cmd=new SqlCommand("Select * from Employees", cn);da.SelectCommand = cmd;da.Fill(DataSet1);

Working with Data Adapters (Contd.)

Set a connection string

Creating a DataSet object

Creating a SqlDataAdapter object

Passing the SQL query to the command object.

Retrieving records from the Employees tableFilling the dataset with records from the Employees table

Page 6: Ado.net session05

Slide 6 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

A data adapter handles data transfer between the database and the dataset through its properties and methods, and displays data through the process of table mapping. After a dataset has been created, the data adapter uses the process of table mapping to map the columns in the database table with the dataset columns. A data adapter uses the TableMappings property, a collection of DatatableMapping objects that is used for mapping between the database table and the DataTable object in the dataset.

Working with Data Adapters (Contd.)

Page 7: Ado.net session05

Slide 7 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

A major challenge related to data access is that more than one user might need to simultaneously access data in a database. Another challenge is more than one user might need to access the data anytime, anywhere. This challenge can be overcome by implementing database locking while a transaction is executing. However, if database locking is not implemented, it can lead to data concurrency conflicts that arise from multiple updates being performed on the database.

Working with Data Adapters (Contd.)

Page 8: Ado.net session05

Slide 8 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

Resolving data concurrency conflicts is a business decision, with the following choices:

Prioritized on time; first update winsPrioritized on time; last update winsPrioritized on rolePrioritized on locationUser resolves the conflict

Working with Data Adapters (Contd.)

Page 9: Ado.net session05

Slide 9 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

A significant way to increase the performance of data updates is to update and send the changes to the database in batches. This is known as batch updates. Batch updates are performed by using the UpdateBatchSize property of the SqlDataAdapter object. By default, the UpdateBatchSize property is set to 1.One way to confirm that the changes are being sent to the database server in batches is to add a RowUpdated event to the SqlDataAdapter object. This event will show the number of rows affected in the last batch.

Working with Data Adapters (Contd.)

Page 10: Ado.net session05

Slide 10 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

ADO.NET provides support for classes that can create any provider-specific objects. These classes are known as the DbProviderFactories classes. The DbProviderFactories class contains a method called GetFactoryClasses that returns a data table, which is populated with data from various providers, as shown in the following figure.

Working with Data Adapters (Contd.)

Page 11: Ado.net session05

Slide 11 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

_______ class can create any data provider-specific object.1. DbCommand2. DbProviderFactories3. DbParameter4. DbConnection

Just a minute

Answer:2. DbProviderFactories

Page 12: Ado.net session05

Slide 12 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

Problem Statement:The HR Manager of Tebisco needs to update the number of vacancies, for those positions where the number of vacancies is not more than 10. This updation will be based on the current position code of employees.As a part of the development team, you need to execute two queries, one to retrieve the number of vacancies per position code, and the other to update the number of vacancies on the basis of the current position code of employees. You need to develop an application that can execute both queries on a single database connection.Hint: You need to access the InternalJobPosting and Employee tables of the HR database.

Demo: Manipulating Data in a Connected Environment

Page 13: Ado.net session05

Slide 13 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

In this session, you learned that: A data adapter, which is a part of the connected environment, retrieves data from a database into a dataset. The data adapters that can be configured to connect to a database in Visual Studio .NET are:

SqlDataAdapterOleDbDataAdapterOdbcDataAdapterOracleDataAdapter

Summary

Page 14: Ado.net session05

Slide 14 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

The following properties and methods of a data adapter can be used to perform the various operations on a database:

SelectCommandInsertCommandUpdateCommandDeleteCommandFill()Update()

A data adapter handles data transfer between the database and the dataset through its properties and methods, and displays the data through the process of table mapping.

Summary (Contd.)

Page 15: Ado.net session05

Slide 15 of 15Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 5

Resolving data concurrency conflicts is a business decision, with the following choices:

Prioritized on time; first update winsPrioritized on time; last update winsPrioritized on rolePrioritized on locationUser resolves the conflict

A significant way to increase the performance of data updates is to update and send the changes to the database in batches. This is known as batch updates.ADO.NET provides support for classes that can create any provider-specific objects, such as SqlClient, Odbc, OracleClient, and OleDb. These classes are known as DbProviderFactories classes.

Summary (Contd.)