An Introduction to ADO.Net
-
Upload
amith-d-raikar -
Category
Documents
-
view
99 -
download
2
Transcript of An Introduction to ADO.Net
![Page 1: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/1.jpg)
An Introduction to ADO.Net
![Page 2: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/2.jpg)
What is ADO.Net?
• The data access classes for the .Net framework
• Designed for highly efficient data access
• Support for XML and disconnected record sets
2C#.Net and Web Technologies
![Page 3: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/3.jpg)
And the .Net framework?
• A standard cross language interface• Encapsulation of services, classes
and data types• Uses XML for data representation
3C#.Net and Web Technologies
![Page 4: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/4.jpg)
Where does ADO sit?V
isual S
tud
io .N
ET
VB C# C++ Jscript …
Common Language Specification
ASP.Net Windows Forms
ADO.Net XML.Net
Base Class Library
Common Language Runtime (CLR)
Windows COM+ Services
4C#.Net and Web Technologies
![Page 5: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/5.jpg)
What happened to ADO?
• ADO still exists.• ADO is tightly coupled to client
server architectures• Needs COM marshalling to pass data
between tiers• Connections and locks are typically
persisted
5C#.Net and Web Technologies
![Page 6: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/6.jpg)
ADO / ADO.Net ComparisonsFeature ADO ADO.Net
In memory data storage
Recordset objectMimics single table
Dataset objectContains DataTables
Data Reads Sequential Sequential or non-sequential
Data Sources
OLE/DB via the Connection object
Managed provider calls the SQL APIs
6C#.Net and Web Technologies
![Page 7: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/7.jpg)
ADO / ADO.Net ComparisonsFeature ADO ADO.Net
Disconnected data
Limited support, suitable for R/O
Strong support, with updating
Passing datasets
COM marshalling DataSet support for XML passing
Scalability Limited Disconnected access provides scalability
7C#.Net and Web Technologies
![Page 8: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/8.jpg)
ADO.Net object model
DataAdapter
Command
DataSet
Errors Collection
Connection Parameters
Data Source
Fill
Update
Sele
ctC
om
mand
Inse
rtC
om
man
d
Upd
ate
Com
mand
Dele
teC
om
man
d
8C#.Net and Web Technologies
![Page 9: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/9.jpg)
Namespaces
• System.Data & System.Data.Common
• System.Data.SqlClient &System.Data.OleDB
• System.Data.SqlTypes• System.XML & System.XML.Schema
9C#.Net and Web Technologies
![Page 10: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/10.jpg)
Using Namespaces
• VB.NetImports System.DataImports System.Data.SqlClientDim sqlAdp as SqlDataAdapter
• C#using System.Data;using System.Data.SqlClient;SqlDataAdapter sqlAdp= new SqlDataAdapter();
10C#.Net and Web Technologies
![Page 11: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/11.jpg)
SQL Namespace Objects
• using System.Data.SqlClient;• SqlConnection• SqlCommand• SqlDataReader• SqlDataAdapter• SqlParameter• SqlParameterCollection• SqlError• SqlErrorCollection• SqlException• SqlTransaction• SqlDbType
11C#.Net and Web Technologies
![Page 12: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/12.jpg)
Connecting to SQL
• using System.Data.SqlClient;
string sConnectionString = "Initial Catalog=Northwind; Data Source=localhost; Integrated Security=SSPI;";
SqlDataAdapter sqlAdp= new SqlDataAdapter(sConnectionString);
sqlAdp.Close();sqlAdp.Dispose();
12C#.Net and Web Technologies
![Page 13: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/13.jpg)
Connection Pooling
• ADO.Net pools connections.When you close a connection it is released back into a pool.
• SqlConnection conn = new SqlConnection();conn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=northwind";conn.Open(); // Pool A is created.
• SqlConnection conn = new SqlConnection();conn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=pubs";conn.Open(); // Pool B is created because the connection strings differ.
• SqlConnection conn = new SqlConnection();conn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=northwind";conn.Open(); // The connection string matches pool A.
13C#.Net and Web Technologies
![Page 14: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/14.jpg)
Getting data
• SqlCommandExecuteReaderExecuteNonQueryExecuteScalarExecuteXMLReader
• SqlDataAdapterDataSet
14C#.Net and Web Technologies
![Page 15: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/15.jpg)
Using the command object
• SqlCommandMultiple constructors
• New()• New(cmdText)• New(cmdText, connection)• New(cmdText, connection, transaction)
15C#.Net and Web Technologies
![Page 16: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/16.jpg)
Using the command object
• string sSelectQuery = "SELECT * FROM Categories ORDER BY CategoryID";string sConnectionString = "Initial Catalog=Northwind; Data Source=localhost; Integrated Security=SSPI;";SqlConnection objConnect = new SqlConnection(sConnectString);SqlCommand objCommand = new SqlCommand(sSelectQuery, objConnect);objCommand.CommandTimeout = 15;objCommand.CommandType = CommandType.Text;
objConnect.Open();
DataReader drResults;drResults = objCommand.ExecuteReader()
drResults.Close();objConnect.Dispose();
16C#.Net and Web Technologies
![Page 17: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/17.jpg)
Command Properties
• .CommandType• .CommandTimeout• .CommandText• .Connection• .Transaction
17C#.Net and Web Technologies
![Page 18: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/18.jpg)
Command Methods
• .CreateParameter(…)• .ExecuteReader()• .ExecuteNonQuery()• .ExecuteXMLReader()• .ExecuteScaler()• .Prepare()
18C#.Net and Web Technologies
![Page 19: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/19.jpg)
The DataReader object
• DataReader objects are highly optimised for fast, forward only enumeration of data from a data command
• A DataReader is not disconnected
19C#.Net and Web Technologies
![Page 20: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/20.jpg)
The DataReader object
• Access to data is on a per record basis.
• Forward only• Read only• Does support multiple recordsets
20C#.Net and Web Technologies
![Page 21: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/21.jpg)
Creating a data reader
SqlDataReader sqlReader; sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read()) { // process, sqlReader("field")}sqlReader.Dispose();
21C#.Net and Web Technologies
![Page 22: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/22.jpg)
Multiple Reads
• Nest your queries.“SELECT COUNT(*) AS RecordCount FROM CUSTOMERS;SELECT * FROM CUSTOMERS”
• Thenwhile (sqlReader.Read()) { nCount = int(sqlReader.GetValue(0)); }if (sqlReader.NextResult()) { // Process the full record set.}
22C#.Net and Web Technologies
![Page 23: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/23.jpg)
Multiple Reads
• Nest your queries.“SELECT COUNT(*) AS RecordCount FROM CUSTOMERS;SELECT * FROM CUSTOMERS”
• Thenwhile (sqlReader.Read()) { nCount = int(sqlReader.GetValue(0)); }if (sqlReader.NextResult()) { // Process the full record set.}
23C#.Net and Web Technologies
![Page 24: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/24.jpg)
DataSets
• In-memory representation of data contained in a database/XML
• Operations are performed on the DataSet, not the data source
• Can be created programmatically, using a DataAdapter or XML schema and document (or any mixture)
24C#.Net and Web Technologies
![Page 25: An Introduction to ADO.Net](https://reader033.fdocuments.in/reader033/viewer/2022061214/549e24ecb37959af618b45eb/html5/thumbnails/25.jpg)
Creating DataSets
• Setup SqlConnection• Setup a SqlDataAdapter• Create a DataSet• Call the .Fill() method on the DA
25C#.Net and Web Technologies