You are on page 1of 37

Muhammad Danish Iqbal Kh

Contents
 What is ADO.Net?
 What happened to ADO?  The ADO.Net object structure

 Connecting
 Commanding  Readers and DataSets

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

classes and data types  Uses XML for data representation .Net framework?  A standard cross language interface  Encapsulation of services.And the .

Net ADO.Net Windows Forms XML.Net Visual Studio .Where does ADO sit? VB C# C++ Jscript … Common Language Specification ASP.NET Base Class Library Common Language Runtime (CLR) Windows COM+ Services .

 ADO is tightly coupled to client server architectures  Needs COM marshalling to pass data between tiers  Connections and locks are typically persisted .What happened to ADO?  ADO still exists.

Net Comparisons Feature ADO ADO.Net In memory Recordset object Dataset object data storage Mimics single table Contains DataTables Data Reads Sequential Sequential or nonsequential Managed provider calls the SQL APIs Data Sources OLE/DB via the Connection object .ADO / ADO.

ADO / ADO.Net Strong support. with updating Passing datasets Scalability COM marshalling DataSet support for XML passing Limited Disconnected access provides scalability .Net Comparisons Feature Disconnected data ADO Limited support. suitable for R/O ADO.

NET Data Provider OLE DB Provider Other DB ODBC ..NET Data Providers SQL .NET Data Provider SQL SERVER Client OLE DB .NET Data Provider ODBC Driver Other DB .

Data Provider Functionality Client .Net Data Provider Connection Command Rows DataReader DataSet DataAdapter database .

ADO.Net object model DataAdapter UpdateCommand DeleteCommand InsertCommand Fill DataSet SelectCommand Update Errors Collection Command Connection Data Source Parameters .

XML.SqlTypes  System.Namespaces  System.Data & System.Common  System.XML & System.Data.Data.OleDB  System.Data.Schema .Data.SqlClient & System.

using System.Net (not case sensitive) Imports System.Data Imports System.SqlClient.Data.Data.Data. SqlDataAdapter sqlAdp= new SqlDataAdapter().Using Namespaces  VB. .SqlClient Dim sqlAdp as SqlDataAdapter  C# (case sensitive) using System.

 SqlConnection  SqlCommand  SqlDataReader  SqlDataAdapter  SqlParameter  SqlParameterCollection  SqlError  SqlErrorCollection  SqlException  SqlTransaction (Roleback possible)  SqlDbType .SQL Namespace Objects  using System.Data.SqlClient.

SqlDataAdapter sqlAdp= new SqlDataAdapter(sConnectionString).Data.SqlClient. Integrated Security=SSPI. .Dispose(). Data Source=localhost. string sConnectionString = "Initial Catalog=Northwind.Close().". sqlAdp.Connecting to SQL  using System. sqlAdp.

 SqlConnection conn = new SqlConnection().Initial Catalog=pubs".Initial Catalog=northwind". conn.ConnectionString = "Integrated Security=SSPI. conn.Open().Open(). When you close a connection it is released back into a pool. .ConnectionString = "Integrated Security=SSPI.ConnectionString = "Integrated Security=SSPI. conn.  SqlConnection conn = new SqlConnection(). // Pool A is created.Net pools connections.Open().Initial Catalog=northwind". conn. // The connection string matches pool A. conn.Connection Pooling  ADO.  SqlConnection conn = new SqlConnection(). // Pool B is created because the connection strings differ. conn.

Getting data  SqlCommand ExecuteReader ExecuteNonQuery ExecuteScalar ExecuteXMLReader  SqlDataAdapter DataSet .

connection)  New(cmdText. connection. transaction) .Using the command object  SqlCommand Multiple constructors  New()  New(cmdText)  New(cmdText.

". SqlCommand objCommand = new SqlCommand(sSelectQuery. Data Source=localhost. SqlConnection objConnect = new SqlConnection(sConnectString). drResults = objCommand.Dispose(). objConnect.Close(). /* objCommand.Open().CommandTimeout = 15. objCommand. SqlDataReader drResults.ExecuteReader() drResults. string sConnectionString = "Initial Catalog=Northwind. Integrated Security=SSPI.Text. .CommandType = CommandType. objConnect).Using the command object    string sSelectQuery = "SELECT * FROM Categories ORDER BY CategoryID". */ objConnect.

ExecuteNonQuery() .Command Methods  .ExecuteScaler() .Returns # of Rows Affected  .ExecuteXMLReader() .Returns XMLReader Object to Read XML documentation  .g. .Returns a Single Value e.ExecuteReader() .Returns DataReader  . SQL SUM function.

forward only enumeration of data from a data command  A DataReader is not disconnected .The DataReader object  DataReader objects are highly optimised for fast.

 Forward only  Read only  Does support multiple recordsets .The DataReader object  Access to data is on a per record basis.

while (sqlReader. .Dispose().Read()) { // process. sqlReader = sqlCommand.ExecuteReader().Creating a data reader SqlDataReader sqlReader. sqlReader("field") } sqlReader.

Other Methods  GetString(). GetInt() etc.  GetValues()  IsDBNull()  GetSchemaTable() .  GetSqlString(). GetSqlInt32() etc.

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) .

Creating DataSets  Setup SqlConnection  Setup a SqlDataAdapter  Create a DataSet  Call the .Fill() method on the DA .

DataAdapters  Pipeline between DataSets and data sources  Geared towards functionality rather than speed  Disconnected by design  Supports select. insert. delete. update commands and methods .

DataAdapters  Must always specify a select command  All other commands can be generated or specified .

sqlConnection). DataSet sqlDS = new DataSet("authorsTable"). sqlDA. .Using the DataAdapter SQLDataAdapter sqlDA = new SqlDataAdapter().Fill(sqlDS. "authorsTable"). sqlDA.SelectCommand = new SqlCommand ("select * from authors“.

UpdateCommand and DeleteCommand  Call GetChanges to seperates the updates. adds and deletes since the last sync.DataAdapters  For speed and efficiency you should set your own InsertCommand. . Then sync each type.

DataTables  A DataSet contains one or more DataTables. DataColumns. .  Fields are held within the DataTable.  And in DataRows.

Sets. Tables and Rows DataSet DataTable DataTable DataRow DataRow .

modify and update  Search  Apply views  Compare  Clear  Clone and Copy .Using DataTables With a DataTable we can  Insert.

Net  Tables within a DataSet can now have relationships. with integrity. .  Supports cascading updates and deletes.DataRelations  New to ADO.

DataViews  Like a SQL view  Single. . or multiple tables  Normally used with GUI applications via Data Binding.

Net Programmer’s Reference Bilbija.org/sql/  My email : desai8@uwindsor. Wrox Press  http://oberon.References  ADO.idunno.ca . Dickenson et al.

Muhammad Danish Iqbal Kh .

%.907094/8 0939 09. O 09'.-0 O 09$973 . O 09$6$973 09$639 09.02.:08 O 8: O 09$.

80..9.9.-..30/ 3./.439.$098 O 3 20247705708039.94341/.9.

9.7050714720/4390.9.22..5907478. .3//4.70.0 O ..:2039 47./.9.90/5747.3-0.9438.$09 349 90/.9.02.84:7. :83.329:70 . O 507.

$09 O .9..70.$6.93.90  2094/4390 .$098 O $09:5$64330.9.9.90.5907 O 70./.943 O $09:5.

.3/2094/8 .3//.59078 O !5030-09003.90/-/083 O $:554798800.$098.3/8.943.422.9.9.9.7/81:3.90 .84:7.9079.08 O 0./.9 38079 /0090 :5/.70/94.3 8500/ O 8.4330.97.

3-00307.3/ O 4907.90/47 850.3/8.422.8850.9.10/ .422.9..59078 O :89..1..800./.

$0986$30 .$09 .:9478%.9.9.9422.590786 30$6././.-0  .9 1742 .5907  86 $00.3/ 30$6422.9./.3/ 800.:9478 864330.9.5907 $".:9478%.9.&8390.943  .-0  86  86$ .

59078 O 478500/.3//0090883..4:84:/8094:7 4338079422. %03 83.90422.908  .950 ..03./.90890:5/.3/ 0090422.09.090.3/ &5/.3/011.0.3/.3089480507.3/ O .//8.8983.9.

9.439.700/9390.-08 O 0/8.$09.-0 O 3/3.%.%.9.%..9.#48 .4:238 .9.-08 O .9.38430472470.9.

$098 %.#4 .%.3/#48 .-08.#4 .-0 .9.9.9.%.$09 .-0 .9.9.

.%.3 O 38079 24/1.3/:5/.7 O 430.70 O 0.08 O 425.7.%.&83.9..90 O $0.-00.9.-08 9.3/45 . O 55.

$09.-0893...0 70..9.#0.8..9../3:5/.334.943858 939079 O $:554798.3//00908 .9438 O 094 09 O %..908.

9438.$".0 O $30 472:9509.55.:80/9&.'08 O 0.9..9... 3/3 .-08 O 472..

2207 8#010703. .08 O  09!747. 74!7088 O 995.0 -.#010703.0384309.

.

4-0743 /:334 47.

86.

 /08. O 02. ..:3/847 .

aSSVSZ_ ]TS  .