Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

12. Data Access using ADO.Net

Lesson Plan

Today we will learn how our C# applications can interact with database systems. We will start out by looking at the architecture of ADO.Net and its different components. Later we will demonstrate data access in .Net through an application. Finally we will learn about stored procedures and explore the Data Grid control which is commonly used for viewing data.

Introducing ADO.Net

Most of today's applications need to interact with database systems to persist, edit or view data. In .Net, data access services are provided through ADO.Net components. ADO.Net is an object oriented framework that allows you to interact with database systems.

We usually interact with database systems through SQL queries or stored procedures. The best thing about ADO.Net is that it is extremely flexible and efficient. ADO.Net also introduces the concept of a disconnected data architecture. In traditional data access components, you made a connection to the database system and then interacted with it through SQL queries using the connection.

The application stays connected to the DB system even when it is not using DB services. This commonly wastes valuable and expensive database resources, as most of the time applications only query and view the persistent data. ADO.Net solves this problem by managing a local buffer of persistent data called a data set.

Your application automatically connects to the database server when it needs to run a query and then disconnects immediately after getting the result back and storing it in the dataset. This design of ADO.Net is called a disconnected data architecture and is very much similar to the connectionless services of HTTP on the internet. It should be noted that ADO.Net also provides connection oriented traditional data access services.

Traditional Data Access Architecture

230

Programmers Heaven: C# School

ADO.Net Disconnected Data Access Architecture

Another important aspect of disconnected architecture is that it maintains a local repository of data in the dataset object. The dataset object stores the tables, their relationship and their different constraints. The user can perform operations like update, insert and delete on this dataset locally, and the changes made to the dataset are applied to the actual database as a batch when needed. This greatly reduces network traffic and results in better performance.

Different components of ADO.Net

Before going into the details of implementing data access applications using ADO.Net, it is important to understand its different supporting components or classes. All generic classes for data access are contained in the System.Data namespace.

Class

Description

DataSet

The DataSet is a local buffer of tables or a collection of disconnected record sets.

DataTable

A DataTable is used to contain data in tabular form using rows and columns.

DataRow

Represents a single record or row in a DataTable.

DataColumn

Represents a column or field of a DataTable.

DataRelation

Represents the relationship between different tables in a data set..

Constraint

Represents the constraints or limitations that apply to a particular field or column.

ADO.Net also contains some database specific classes. This means that different database system providers may provide classes (or drivers) optimized for their particular database system. Microsoft itself has provided the specialized and optimized classes for their SQL server database system. The name of these classes start with 'Sql' and are contained in the System.Data.SqlClient namespace.

Similarly, Oracle has also provides its classes (drivers) optimized for the Oracle DB System. Microsoft has also provided the general classes which can connect your application to any OLE supported database server. The name of these classes start with 'OleDb' and these are contained in the System.Data.OleDb namespace. In fact, you can use OleDb classes to connect to SQL server or Oracle database; using the database specific classes generally provides optimized performance, however.

Class

Description

231

Programmers Heaven: C# School

SqlConnection, OleDbConnection

Represents a connection to the database system.

SqlCommand, OleDbCommand

Represents SQL query.

SqlDataAdapter, OleDbDataAdapter

A class that connects to the database system, fetches the record and fills

 

the dataset.

SqlDataReader, OleDbDataReader

A stream that reads data from the database in a connected design.

SqlParameter, OleDbParameter

Represents a parameter to a stored procedure.

A review of basic SQL queries

Here we present a brief review of four basic SQL queries.

SQL SELECT Statement

This query is used to select certain columns of certain records from one or more database tables.

SELECT * from emp

selects all the fields of all the records from the table named 'emp'

SELECT empno, ename from emp

selects the fields empno and ename of all the records from the table named 'emp'

SELECT * from emp where empno < 100

selects all those records from the table named 'emp' where the value of the field empno is less than 100

SELECT * from article, author where article.authorId = author.authorId

selects all those records from the tables named 'article' and 'author' that have the same value of the field authorId

SQL INSERT Statement

This query is used to insert a record into a database table.

INSERT INTO emp(empno, ename) values(101, 'John Guttag')

inserts a record in to the emp table and set its empno field to 101 and its ename field to 'John Guttag'

SQL UPDATE Statement

This query is used to modify existing records in a database table.

232

Programmers Heaven: C# School

UPDATE emp SET ename = 'Eric Gamma' WHERE empno = 101

updates the record whose empno field is 101 by setting its ename field to 'Eric Gamma'

SQL DELETE Statement

This query is used to delete existing record(s) from a database table.

DELETE FROM emp WHERE empno = 101

deletes the record whose empno field is 101 from the emp table

Performing common data access tasks with ADO.Net

Enough review and introduction! Let's start something practical. Now we will build an application to demonstrate how common data access tasks are performed using ADO.Net.

We will use the MS SQL server and MS Access database systems to perform the data access tasks. SQL Server is used because probably most of the time you will be using MS SQL server when developing .Net applications.

For SQL server, we will be using classes from the System.Data.SqlClient namespace. Access is used to demonstrate the OleDb databases. For Access we will be using classes from the System.Data.OleDb namespace.

In fact, there is nothing different in these two approaches for developers and only two or three statements will be different in both cases. We will highlight the specific statements for these two using comments like:

// For SQL server

SqlDataAdapter dataAdapter = new

SqlDataAdapter(commandString, conn);

// For Access

OleDbDataAdapter dataAdapter = new

OleDbDataAdapter(commandString, conn);

For the example code, we will be using a database named 'ProgrammersHeaven'. The database will have a table named 'Article'. The fields of the table 'Article' are:

Field Name

Type

Description

artId

(Primary Key)Integer

The unique identifier for article

title

String

The title of the article

topic

String

Topic or Series name of the article like 'Multithreading in

 

 

Java' or 'C# School'

233

 

 

Соседние файлы в папке books