Session 8 - Accessing and Manipulating Cloud Data 26062024 014442pm
Session 8 - Accessing and Manipulating Cloud Data 26062024 014442pm
Cloud Data
Session 8
Data Access Components
Requests data
Database
Sends data
User Server Running
Requests data
Database
Error Message
Client Software Server Down
DAC
(ODBC, OLE DB, Database
ADO, ADO.net)
Data Access Components [Cont…]
• The client can access the database using different data access
components. Some of the data access components are:
• ODBC
• OLE DB
• DAO
• RDO
• ADO
• ADO.NET
Various Data Access Technologies
Entity
ADO.NET
Framework
ADO.NET 1-2
ADO.NET:
– Consists of a set of classes provided by the .NET Framework that you can
use to access the data stored in a database.
Connection
Command
DataReader
DataAdapter
Connection Object 1-3
In this code:
– A SqlConnection object is created and its ConnectionString property is set.
– The connection string contains the DataSource property that specifies the address of
the data source.
– The InitialCatalog property specifies the name of the database to access.
– The SSPI value of the IntegratedSecurity property specifies that the Windows
user account should be used to connect to the database.
– The False value of the PersistSecurityInfo property specifies that the
authentication information used to connect to the database should be discarded once
the connection is established.
Connection Object 3-3
– This code creates a SqlCommand object initialized with a SQL SELECT statement and
the opened SqlConnection object.
Command Object - Methods
Methods Description
This method is used to execute an sql statement that is specified as the command text
ExecuteNonQuery for the command object. It also returns the number of rows that have been affected or
retrieved by the SQL statement.
This method is used to execute a stored procedure that is specified as the command text
ExecuteReader for the command object. It also creates a DataReader object with the retrieved data.
ExecuteScalar This method is typically used to execute a SQL query that returns a single value, such as
an aggregate function (e.g., COUNT, SUM, AVG) or a scalar value from a table.
Data Reader Object 1-2
Once you have created the data reader object, you can
retrieve a single row of data at a time by calling the
Read()method of the SqlDataReader object.
Following code shows reading the first row stored in the data
reader object:
dataReader.Read();
Fetching Single Value
count(empno)
Client
Returns a number Application
Example:
Instead, they cache data from the database and after that, the connection
can be closed.
The Dataset object requires the data adapter to retrieve data from the
data source.
DataSet - Example
Client requests data from Server
Client Server
DataSet
Data Set Object 2-3
In this code:
– The data from the data source is retrieved by the data adapter and
filled into the dataset.
Data Set Object 3-3
For example, you can use a GridView as a User Interface (UI) control.
gvwEmp.DataSource = ds;
gvwEmp.DataBind();
DataView
• Presentation layer for the data stored in DataTable
• Provides view of DataTable for sorting, filtering and searching
• Can be used to view a subset of the data stored in DataTable
• There can be two controls on the same DataTable, which
provide different view of the data
DataView [Example]
SqlConnection sqlcon;
DataView dv;
sqlcon= new SqlConnection("server=SQLDB; uid=sa; pwd=; database=pubs");
SqlDataAdapter sqlcom = new SqlDataAdapter("select * from employee", sqlcon);
DataSet ds = new DataSet();
sqlcom.Fill(ds, "employee");
dv = new DataView (ds.Tables ["employee"]);
dv.RowFilter = "job_id >10";
dv.Sort = "lname DESC";
DataGrid1.DataSource=dv;
SqlTransaction Class
• The SqlTransaction class in C# is used to manage database transactions in
SQL Server using the ADO.NET data provider.
• A transaction is a unit of work that must be completed in its entirety or
not at all, ensuring data integrity and consistency.
Asynchronous Operations with ADO.NET
• Database operations can take a long time to complete.
• With ADO.NET, you can use asynchronous operations to execute long-running queries
without creating and blocking a managed thread.
• ADO.NET supports asynchronous operations:
• Background execution for long-running operations
• Freeing up managed threads until the database responds
• You can use asynchronous command operations:
• ExecuteNonQueryAsync
• ExecuteReaderAsync
• ExecuteScalarAsync
• In ASP.net Core, the asynchronous methods return a Task<T> object, where the generic type
parameter T is the type returned by the corresponding synchronous method. For example, the
ExecuteReaderAsync method returns a Task<DbDataReader> object, whereas the
corresponding synchronous method, ExecuteReader, returns a DbDataReader object.
Cloud Computing
23
Spring 2024
Cont…
• In ASP.NET Core, the await keyword is used to asynchronously wait for the
completion of an asynchronous operation, typically represented by a Task or
Task<T> object.
• The await keyword introduced in C# 5 to schedule a continuation when the
operation completes. You can also use the Task.ContinueWith method to provide
a delegate as the continuation of the task
• Task.ContinueWith method is used to chain multiple asynchronous operations
together, allowing you to specify a continuation task that will be executed after
the original task has completed.
• Other asynchronous methods:
• DbConnection.OpenAsync
• DbDataReader.ReadAsync
Cloud Computing
24
Spring 2024
Cont…
using (var connection = new SqlConnection(connectionString))
{
await connection.OpenAsync();
using (var command = new SqlCommand(commandString, connection))
{
using (SqlDataReader reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
}
}
}
}
Cloud Computing
25
Spring 2024