1
Chapter 5
Configuring Connections and
Connecting to Data
Week ?
Chapter 5
Lesson 0: ADO.NET introduce
Lesson 1: Creating and Configuring
Connection Objects
Lesson 2: Connecting to Data Using
Connection Objects
Lesson 3: Working with Connection Pools
Lesson 4: Handling Connection Errors
Lesson 5: Enumerating the Available SQL
Servers on a Network
2
Overview of ADO.NET
with the .NET Framework
Introdution to ADO.NET
Introdution to ADO.NET
Universal Data Access (UDA)
Microsoft’s strategy for accessing data for
multiple providers
Goal is to access any type of data from any
application on any type of computer
Introdution to ADO.NET
Introdution to ADO.NET
3
OLEDB
Technology designed to implement the UDA
concept
Provides an standardized object-oriented
interface
Allows access data from any source
Library functions
Hard to use
Introdution to ADO.NET
Introdution to ADO.NET
ADO.NET
Important Core of the .NET Framework
Microsoft’s latest database object model
Data access solution in .NET
Allows VB programmers to use a standard
set of objects to refer to data from any
source: SQL Server, Oracle, OLE DB &
ODBC (UDA…)
ActiveX Data Objects
ADO
ActiveX Data Objects
ADO
4
Power of ADO.NET
.NET framework is integrated with XML, stored and transferred in
XML
Can save and load from XML
XML is the way to exchange data between business
any programming language use it.
Create XML Schema
Good use in multitier applications
Disconnected dataset
Introdution to ADO.NET
Introdution to ADO.NET
XML is an industry standard
format for storing and
transferring data over
multiple platforms.
XML is an industry standard
format for storing and
transferring data over
multiple platforms.
ADO.NET and XML
Working together in n-tier architech
Introdution to ADO.NET
Introdution to ADO.NET
5
.NET Data Provider Objects
SelectCommand
InsertCommand
UpdateCommand
DeleteCommand
Connection
.NET Data Provider
Command
Data Adapter
DataReader
Generic classes
Connection
Command.
DataReader
DataAdapter
DataSet
Core ADO.NET Namespaces
System.Data
System.Data.OleDb
System.Data.SqlClient
6
Importing the ADO.NET
Namespaces
Needed to build a data access application
For OLE DB: (MS Access)
Imports System.Data
Imports System.Data.OleDB
For SQL Server:
Imports System.Data
Imports System.Data.SQLClient
Lesson 1: Creating and Configuring Connection
Objects
What Is a Connection Object?
7
What Is a Connection Object?
8
Connection classes
How to create connection?
Creating Connections in Server Explorer
Lab: P206
Creating Connections Using Data Wizards
Lab: P207
Creating Connection Objects
Programmatically
(see next page)
9
How to create connection?
Creating Connection Objects
Programmatically (page 204-205)
Connection properties
10
Connection methods
And … Questions?
11
Lesson 2: Connecting to Data Using
Connection Objects
Opening and Closing Data Connections
Connection Events
Opening and Closing Data
Connections
Open and Close methods.
Connection must contain a valid connection
string.
State of the connection change
12
Connection Events
Connection open and close method
Where?
When
13
Exer: Open and Close methods.
Lab page 213
Questions
Connect to SQL Server
Connect to MS Access
Lesson 3: Working with Connection
Pools
What Is Connection Pooling?
Controlling Connection Pooling Options
Configuring Connections to Use Connection
Pooling
14
What Is Connection Pooling?
Reuse of existing connections
Reduce the overhead when creating and
disposing of connections
Connections have the same configuration
Each time call SqlConnection.Open with a
unique connection string, a new pool is
created
Controlling Connection Pooling
Options
is enabled by default when creating
ADO.NET connection
set Pooling=False in your connection string
Connection Pooling Connection String
Keywords (Page 227)
Min Pool Size, Max Pool Size
15
Configuring Connections to Use
Connection Pooling (page 229)
Configuring Connection Pooling with SQL Server
Connections
Manual config
Data Source=SqlServerName;Initial
Catalog=DatabaseName; Integrated Security=True;Min
Pool Size=5
Configuring Connection Pooling with OLE DB
Connections
Automatic by Provider
Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\DataSources\Nwind.mdb;OLE DB Services=-1
(See page 229)
DEMO POOL with SQLSERVER
Lesson 4: Handling Connection Errors
Handle exceptions when connecting to a
database.
Use the SqlException (OLEDBException)
class to detect connection errors
Use the SqlError (OleDBError) class to detect
connection errors.
16
Error collections
SQL Server returns error, the Data Provider
for SQL Server throws a SqlException that
you can catch in your application
When SqlException is thrown, inspect the
SqlException.Errors collection of errors
Error collections (page 233)
17
Exer:
Lab page 232
Lesson 5: Enumerating the Available SQL
Servers on a Network
Using Instance property of the
SqlDataSourceEnumerator class
call the GetDataSources method.
The GetDataSources method returns a Data-
Table that contains information for each SQL
server that is visible on the network.
18
Enumerating the Available SQL
(page 237)
Enumerating the Available SQL
Lab page 238
19
And … Questions?
And … Questions?
What about?
UDA
OLEDB
DAO, ADO and ADO.NET
Connection
ConnectionPool
ConnectionString
What informations in Connection data object?