Intro To
Intro To
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
Base Class Library Common Language Runtime (CLR) Windows COM+ Services
architectures Needs COM marshalling to pass data between tiers Connections and locks are typically persisted
Data Reads
Sequential
Sequential or nonsequential
Managed provider calls the SQL APIs
Data Sources
Passing datasets
Scalability
Client
OLE DB Provider
Other DB
ODBC Driver
Other DB
Command
Rows
DataReader
DataSet
DataAdapter
database
DataSet
SelectCommand
Update
Errors Collection
Namespaces
System.Data & System.Data.Common
System.Data.SqlClient &
Using Namespaces
VB.Net (not case sensitive)
Imports System.Data Imports System.Data.SqlClient Dim sqlAdp as SqlDataAdapter C# (case sensitive) using System.Data; using System.Data.SqlClient; SqlDataAdapter sqlAdp= new SqlDataAdapter();
SqlDataAdapter
SqlParameter SqlParameterCollection SqlError
SqlErrorCollection
SqlException SqlTransaction (Roleback possible) SqlDbType
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();
Connection Pooling
ADO.Net pools connections.
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.
Getting data
SqlCommand
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(); SqlDataReader drResults; drResults = objCommand.ExecuteReader() drResults.Close(); objConnect.Dispose();
Command Methods
.ExecuteReader() - Returns DataReader
.ExecuteNonQuery() - Returns # of Rows Affected .ExecuteXMLReader() - Returns XMLReader Object to Read XML documentation .ExecuteScaler() - Returns a Single Value e.g. SQL SUM function.
fast, forward only enumeration of data from a data command A DataReader is not disconnected
Other Methods
GetString(), GetInt() etc.
GetSqlString(), GetSqlInt32() etc. GetValues() IsDBNull() GetSchemaTable()
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
DataAdapters
For speed and efficiency you should set your
own InsertCommand, UpdateCommand and DeleteCommand Call GetChanges to seperates the updates, adds and deletes since the last sync. Then sync each type.
DataTables
A DataSet contains one or more DataTables.
Fields are held within the DataTable. And in DataRows, DataColumns.
Using DataTables
With a DataTable we can Insert, modify and update Search Apply views Compare Clear Clone and Copy
DataRelations
New to ADO.Net
Tables within a DataSet can now have
DataViews
Like a SQL view
Single, or multiple tables Normally used with GUI applications via Data
Binding.
References
ADO.Net Programmers Reference