Building Applications using C# and .net...

29
BUILDING APPLICATIONS USING C# AND .NET FRAMEWORK (OBJECT-ORIENTED PROGRAMMING, X428.6) Professional Program: Data Administration and Management Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 8

Transcript of Building Applications using C# and .net...

BUILDING APPLICATIONS USING C# AND .NET

FRAMEWORK

(OBJECT-ORIENTED PROGRAMMING, X428.6)

Professional Program: Data Administration and Management

Instructor: Michael Kremer, Ph.D. Technology & Information Management

Class 8

AGENDA

19. Database Programming, Disconnected

Architecture

20. Entity Framework

21. Binding Data to Form Controls

Database Programming, Disconnected Architecture

19.

19.1 DATASET OBJECT

Unlike DataReaders, which are objects of data provider–specific classes, data sets are objects of the class System.Data.DataSet, a distinct ADO.NET component used by all data providers.

DataSet can be used either connected or disconnected from data sources.

Fundamental purpose is to provide a relational view of data stored in an in-memory cache.

DataReader vs. DataSet

To read and display data, use only a data reader, particularly if you are working with large quantities of data.

To manipulate data in any way and then update the database, you need to use a data set.

A data adapter fills a data set by using a data reader; additional resources are needed to save data for disconnected use.

242

19.1 DATASET OBJECT

Overview of DataSet Object

A DataSet is an in-memory data store that can hold numerous tables.

DataSets only hold data and do not interact with a data source.

DataAdapter that manages connections with the data source and gives us disconnected behavior.

Disconnected Mode: people working without network connectivity and making Web sites more scalable.

When updating of data is needed, you need to think of concurrency issues.

Use disconnected data when your information is primarily read only, but consider other alternatives when your requirements call for something more dynamic.

If the amount of data is so large that holding it in memory is impractical, you will need to use DataReader for read-only data.

243

19.1 DATASET OBJECT

DataSet Object Architecture

A DataSet is comprised of the following objects:

DataTable ( Rows, Columns, Constraints)

DataRelation

“Connected” part only shows component needed to manage the

“Disconnected” part of ADO.NET.

To create a DataSet, use the

following syntax:

DataSet constructor does not

require any parameters.

One overload that accepts a

string for the name of the DataSet.

244

Database

Connected

Disconnected

Connection

Data Adapter

SELECT command

UPDATE command

INSERT command

DELETE command

CommandBuilder

DataSet

DataTables

Rows

Columns

Constraints

DataRelations

DataView

DataReader

19.2 DATATABLE OBJECT

A data table has collections of data rows and data columns.

Access these nested collections via the Rows and Columns

properties of the data table.

A data table can be created in three

different ways:

As an independent table object (without a

data set) created manually in C#:

As an independent table object (without a data set) created through a

data reader object:

As part of data set object created through

the data adapter object:

Accessing columns:

Accessing rows:

245

19.2 DATATABLE OBJECT

246

19.2 DATATABLE OBJECT

Implement application-

wide settings to store

connection string:

Static public class and

expose the connection

string as a get accessor

property.

Add the connection

string to the app.config

file and use the

ConfigurationManager

class to access it.

247

19.2 DATATABLE OBJECT

248

19.2 DATATABLE OBJECT

249

19.3 DATAADAPTER OBJECT

SqlDataAdapter holds the SQL commands and connection object

for reading and writing data.

You initialize it with a SQL select statement and connection

object:

Connection object should have already been instantiated, but

not opened. It is the SqlDataAdapter's responsibility to open and

close the connection during Fill and Update method calls.

Add DML SQL statements after SQLAdapter is instantiated:

via SqlCommandBuilder

via SqlDataAdapter

properties

SQL CommandBuilder infers DML statements from SQL Select.

To manually add DML statements, use SqlDataAdapter

properties.

250

19.3 DATAADAPTER OBJECT

251

19.3 DATAADAPTER OBJECT

252

19.3 DATAADAPTER OBJECT

Updating data using data sets and data adapter is pretty

straightforward. But we have oversimplified things!

You need to think about the likelihood of concurrencies.

ADO.NET provides a fundamental level of concurrency control

that is designed to prevent update anomalies. Basically, a data

set marks all added, modified, and deleted rows.

Update is ignored if

data has changed since

it was read. This is

known as optimistic.

Locking rows at the DB

only works in connected

mode!

253

Entity Framework

20.

20.1 INTRODUCTION TO ENTITY FRAMEWORK

Vision behind ADO.NET Entity Framework (EF) 5.0 is to extend

the level of abstraction for database programming and

completely remove the impedance mismatch between data

models and development languages that programmers use to

write database-oriented software applications.

Allows developers to focus on data through an object model

instead of through the traditional logical/relational data model,

helping abstract the logical data schema into a conceptual

model, a mapping layer, and a logical layer to allow interaction

with that model through a new data provider called EntityClient

EntityClient follows a model similar to familiar ADO.NET objects,

using EntityConnection and EntityCommand objects to return an

EntityDataReader.

254

20.1 INTRODUCTION TO ENTITY FRAMEWORK

Core of ADO.NET EF is its Entity Data Model.

Relational database often stores data in a different format from

what the application can use. forces developers to retrieve

the data in the same structure as that contained in the

database.

ADO.NET EF bridges this gap between data models using

mapping layers. There are three layers active in ADO.NET EF’s

model.

Conceptual layer (XML using Conceptual Schema Definition Language)

Mapping layer (XML using Store Schema Definition Language)

Logical layer (XML using Mapping Schema Language)

These three layers allow data to be mapped from a relational

database to a more object-oriented business model.

255

20.2 IMPLEMENTING ENTITY FRAMEWORK

Add a new item to your

project and select the

Entity Framework

template.

Then a wizard guides you

through the process of

setting up the EF:

Connecting to the database.

Selecting the object to map.

Creating the entire framework.

In the end all the three different layers are created through xml

files.

256

20.2 IMPLEMENTING ENTITY FRAMEWORK

Once the EF model is

setup, programming

against this framework is

fairly simple and does

not involve any

connection, command or

data reader objects.

You simply deal with

entities in an

object-oriented layer.

257

Logical Model

Conceptual Model

Mapping Layer

20.2 IMPLEMENTING ENTITY FRAMEWORK

258

20.2 IMPLEMENTING ENTITY FRAMEWORK

259

Binding Data to Form Controls

21.

21.1 OVERVIEW OF DATA BINDING

There are two main methods of data binding as shown below: Simple Data Binding (single value)

Complex Data Binding (multiple value)

These two methods map to user interface controls, some are designed to display only a single value, whereas others are geared towards displaying complex data.

Automatic methods where you use objects that handle most of the work for you. These objects are: Data Source, Binding Source, Navigation Control

Entity Framework

Or write your own customized code to implement data binding.

The most commonly used controls for single binding are listed below: Label (read-only)

Textbox, Rich Textbox

Checkbox, Radiobutton

DateTimePicker, MonthCalendar

The controls for complex binding are listed below: Listbox

Drop-down, Combobox

DataGridView

260

21.2 SIMPLE DATA BINDING

For a textbox control, you can use the Databindings.Add method

or directly assign a value to the Text property of a textbox control

from a data table object:

In the add method of the databindings object only the first row is

referenced. In the second example you can exactly specify which

row of the datatable you want to reference.

To specify a specific row for the Databindings.Add method create

a DataView object from the

data table and apply a filter:

Binding a scalar value:

If a component implements the IList interface then it is

transformed into an index based collection:

Arrays, DataColumn, DataTable, DataView, DataSet

261

21.2 SIMPLE DATA BINDING

262

21.2 SIMPLE DATA BINDING

263

21.2 COMPLEX DATA BINDING

Complex binding always involves at least multiple column/values, or at most an entire result set consisting of many rows and columns.

Bind data to List or Combobox controls (ctl refers to either control):

A data grid view control is rich data control that allows you to manage the entire data set using the control.

It looks like a spreadsheet, where each individual cell can be edited. To bind a data grid view control, use the following syntax:

There are no other settings needed, the control will read the data source and determine the number of columns and column headers and render the control accordingly.

264

21.2 COMPLEX DATA BINDING

265