connected data classes
DESCRIPTION
TRANSCRIPT
![Page 1: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/1.jpg)
USING ADO.NET CONNECTED CLASSES
![Page 2: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/2.jpg)
Connected Data Classes
DbConnection DbCommand DbDataReader DbDataAdapter DbProviderFactory
![Page 3: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/3.jpg)
Using Provider Classes to Move Data
The classes that are responsible for working with the database for connecting, retrieving, updating, inserting, and deleting data are referred to as provider classes in the framework.
The ADO.NET libraries contain provider classes, which are classes that you can use to transfer data between a data store and the client application.
The Microsoft .NET Framework contains the following data access providers:
OleDb Odbc SqlServer Oracle
![Page 4: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/4.jpg)
Primary Provider Classes and Interfaces in ADO.NET
![Page 5: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/5.jpg)
Getting Started with the DbConnection Object
To access a data store, you need a valid, open connection object.
The DbConnection class is an abstract class from which the provider-specific connection classes inherit.
To create a connection, you must have a valid connection string.
DbConnection connection = new SqlConnection();connection.ConnectionString ="Server=.;Database=pubs;Trusted_Connection=tru
e";connection.Open();//do work hereconnection.Close();
![Page 6: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/6.jpg)
Configuring an ODBC Connection String
Open Database Connectivity (ODBC) is one of the older technologies that the .NET Framework supports, primarily because there are still many scenarios in which the .NET Framework is required to connect to older database products that have ODBC drivers.
![Page 7: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/7.jpg)
Sample ODBC Connection Strings
Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=C:\\Sample\\MySampleFolder;
Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\\Code\\mySampleFolder\\northwind.mdb
Driver={Microsoft ODBC for Oracle}; Server=ORACLE8i7; UID=john; PWD=s3$W%1Xz
Driver={Microsoft Excel Driver (*.xls)}; DBQ=C:\\Samples\\MyBook.xls
DRIVER={SQL Server}; SERVER=MyServer; UID=AppUserAccount; PWD=Zx
%7$ha; DATABASE=northwind;
![Page 8: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/8.jpg)
Configuring an OLEDB Connection String
Another common, but older, technology that is used to access databases is Object Linking and Embedding for Databases (OLEDB).
![Page 9: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/9.jpg)
Sample OleDb Connection Strings
This connection string uses the settings stored in the MyAppData.udl file (the .udl extension stands for universal data link):
FILE NAME=C:\Program Files\MyApp\MyAppData.udl
This connection string uses the Jet driver, which is the Access driver, and opens the demo.mdb database file.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\myApp\demo.mdb;Persist Security Info=False
![Page 10: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/10.jpg)
Configuring a SQL Server Connection String
![Page 11: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/11.jpg)
Configuring a SQL Server Connection String
![Page 12: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/12.jpg)
Sample SQLServer Connection Strings
Persist Security Info=False; Integrated Security=SSPI; database=northwind;
Network Library=DBMSSOCN;Data Source=192.168.1.5,1433; Initial
Catalog=MyDbName;User ID=myUsername; Password= u$2hJq@1
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MyApplication\PUBS.MDF;Integrated Security=True; User Instance=True
![Page 13: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/13.jpg)
Storing the Connection String in the Web Configuration File
<connectionStrings><add name="PubsData“
providerName="System.Data.SqlClient"connectionString= "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|PUBS.MDF; Integrated Security=True; User Instance=True"/></connectionStrings>
ConnectionStringSettings pubs =ConfigurationManager.ConnectionStrings["PubsData"];DbConnection connection = new
SqlConnection(pubs.ConnectionString);
![Page 14: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/14.jpg)
Working with Connection Pools
Connection pooling is the process of reusing existing active connections instead of creating new connections when a request is made to the database.
It involves the use of a connection manager that is responsible for maintaining a list, or pool, of available connections.
When the connection manager receives a request for a new connection, it checks its pool for available connections. If a connection is available, it is returned.
If no connections are available, and the maximum pool size has not been reached, a new connection is created and returned.
If the maximum pool size has been reached, the connection request is added to the queue and the next available connection is returned, as long as the connection timeout has not been reached.
![Page 15: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/15.jpg)
Working with Connection Pools
Connection pooling is controlled by parameters placed into the connection string. The followingis a list of parameters that affect pooling:
Connection Timeout Min Pool Size Max Pool Size Pooling Connection Reset Load Balancing Timeout, Connection
Lifetime Enlist
![Page 16: Connected data classes](https://reader035.vdocuments.net/reader035/viewer/2022081602/549a07e2b479591e4d8b4569/html5/thumbnails/16.jpg)
Using the DbCommand Object
The DbCommand object is used to send one or more Structured Query Language (SQL) statements to the data store.
The DbCommand can be any of the following types:
Data Manipulation Language (DML) Commands that retrieve, insert, update, or delete data
Data Definition Language (DDL) Commands that create tables or other database objects, or modify the database schema
Data Control Language (DCL) Commands that grant, deny, or revoke permissions