+ All Categories
Home > Documents > Intro to Ado.net

Intro to Ado.net

Date post: 07-Apr-2018
Category:
Upload: hammad155
View: 222 times
Download: 0 times
Share this document with a friend

of 37

Transcript
  • 8/3/2019 Intro to Ado.net

    1/37

    Muhammad Danish Iqbal Kh

  • 8/3/2019 Intro to Ado.net

    2/37

    Contents What is ADO.Net?

    What happened to ADO?

    The ADO.Net object structure Connecting

    Commanding

    Readers and DataSets

  • 8/3/2019 Intro to Ado.net

    3/37

    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

  • 8/3/2019 Intro to Ado.net

    4/37

    And the .Net framework? A standard cross language interface

    Encapsulation of services, classes and data

    types Uses XML for data representation

  • 8/3/2019 Intro to Ado.net

    5/37

    Where does ADO sit?

    Vis

    ualStudio

    .NET

    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

  • 8/3/2019 Intro to Ado.net

    6/37

    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

  • 8/3/2019 Intro to Ado.net

    7/37

    ADO / ADO.Net ComparisonsFeature ADO ADO.NetIn memorydata storage

    Recordset objectMimics single table

    Dataset objectContains DataTables

    Data Reads Sequential Sequential or non-sequential

    DataSources

    OLE/DB via theConnection object

    Managed providercalls the SQL APIs

  • 8/3/2019 Intro to Ado.net

    8/37

    ADO / ADO.Net ComparisonsFeature ADO ADO.NetDisconnecteddata

    Limited support,suitable for R/O

    Strong support,with updating

    Passingdatasets

    COM marshalling DataSet support forXML passing

    Scalability Limited Disconnected accessprovides scalability

  • 8/3/2019 Intro to Ado.net

    9/37

    Client

    SQL .NET

    DataProvider

    OLE DB .NET

    DataProvider

    ODBC .NET

    DataProvider

    OLE DB

    Provider

    ODBC

    Driver

    SQL SERVER

    Other DB

    Other DB

    .NET Data Providers

  • 8/3/2019 Intro to Ado.net

    10/37

    Rows

    DataSet

    .Net Data ProviderClient

    Connection Command

    databaseDataAdapter

    DataReader

    Data Provider Functionality

  • 8/3/2019 Intro to Ado.net

    11/37

    ADO.Net object modelDataAdapter

    Command

    DataSet

    Errors Collection

    Connection Parameters

    Data Source

    Fill

    Update

    SelectC

    ommand

    InsertC

    ommand

    Update

    Command

    DeleteCommand

  • 8/3/2019 Intro to Ado.net

    12/37

    Namespaces System.Data & System.Data.Common

    System.Data.SqlClient &

    System.Data.OleDB System.Data.SqlTypes

    System.XML & System.XML.Schema

  • 8/3/2019 Intro to Ado.net

    13/37

    Using Namespaces VB.Net (not case sensitive)Imports System.DataImports System.Data.SqlClient

    Dim sqlAdp as SqlDataAdapter C# (case sensitive)using System.Data;using System.Data.SqlClient;

    SqlDataAdapter sqlAdp= newSqlDataAdapter();

  • 8/3/2019 Intro to Ado.net

    14/37

    SQL Namespace Objects using System.Data.SqlClient; SqlConnection

    SqlCommand

    SqlDataReader

    SqlDataAdapter

    SqlParameter

    SqlParameterCollection

    SqlError SqlErrorCollection

    SqlException

    SqlTransaction (Roleback possible)

    SqlDbType

  • 8/3/2019 Intro to Ado.net

    15/37

    Connecting to SQL using System.Data.SqlClient;

    string sConnectionString =

    "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";

    SqlDataAdapter sqlAdp= newSqlDataAdapter(sConnectionString);

    sqlAdp.Close();sqlAdp.Dispose();

  • 8/3/2019 Intro to Ado.net

    16/37

    Connection Pooling ADO.Net pools connections.

    When you close a connection it is released back into apool.

    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.

  • 8/3/2019 Intro to Ado.net

    17/37

    Getting data SqlCommand

    ExecuteReaderExecuteNonQueryExecuteScalarExecuteXMLReader

    SqlDataAdapterDataSet

  • 8/3/2019 Intro to Ado.net

    18/37

    Using the command object SqlCommand

    Multiple constructors

    New()

    New(cmdText)

    New(cmdText, connection)

    New(cmdText, connection,

    transaction)

  • 8/3/2019 Intro to Ado.net

    19/37

    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();

    SqlDataReader drResults;drResults = objCommand.ExecuteReader()drResults.Close();objConnect.Dispose();

  • 8/3/2019 Intro to Ado.net

    20/37

    Command Methods .ExecuteReader() - Returns DataReader

    .ExecuteNonQuery() - Returns # of Rows Affected

    .ExecuteXMLReader() - Returns XMLReaderObject to Read XML documentation

    .ExecuteScaler() - Returns a Single Value e.g.SQL SUM function.

  • 8/3/2019 Intro to Ado.net

    21/37

    The DataReader object DataReader objects are highly optimised for

    fast, forward only enumeration of data from adata command

    A DataReader is not disconnected

  • 8/3/2019 Intro to Ado.net

    22/37

    The DataReader object Access to data is on a per record basis.

    Forward only

    Read only Does support multiple recordsets

  • 8/3/2019 Intro to Ado.net

    23/37

    Creating a data readerSqlDataReader sqlReader;

    sqlReader =

    sqlCommand.ExecuteReader();while (sqlReader.Read())

    {

    // process, sqlReader("field")}

    sqlReader.Dispose();

  • 8/3/2019 Intro to Ado.net

    24/37

    Other Methods GetString(), GetInt() etc.

    GetSqlString(), GetSqlInt32() etc.

    GetValues() IsDBNull()

    GetSchemaTable()

  • 8/3/2019 Intro to Ado.net

    25/37

    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)

  • 8/3/2019 Intro to Ado.net

    26/37

    Creating DataSets Setup SqlConnection

    Setup a SqlDataAdapter

    Create a DataSet Call the .Fill() method on the DA

  • 8/3/2019 Intro to Ado.net

    27/37

    DataAdapters Pipeline between DataSets and data sources

    Geared towards functionality rather thanspeed

    Disconnected by design

    Supports select, insert, delete, updatecommands and methods

  • 8/3/2019 Intro to Ado.net

    28/37

    DataAdapters Must always specify a select command

    All other commands can be generated orspecified

  • 8/3/2019 Intro to Ado.net

    29/37

    Using the DataAdapterSQLDataAdapter sqlDA =new SqlDataAdapter();

    sqlDA.SelectCommand =new SqlCommand ("select * fromauthors, sqlConnection);

    DataSet sqlDS = newDataSet("authorsTable");sqlDA.Fill(sqlDS, "authorsTable");

  • 8/3/2019 Intro to Ado.net

    30/37

    DataAdapters For speed and efficiency you should set your

    own InsertCommand, UpdateCommand andDeleteCommand

    Call GetChanges to seperates the updates,adds and deletes since the last sync. Thensync each type.

  • 8/3/2019 Intro to Ado.net

    31/37

    DataTables A DataSet contains one or more DataTables.

    Fields are held within the DataTable.

    And in DataRows, DataColumns.

  • 8/3/2019 Intro to Ado.net

    32/37

    Sets, Tables and RowsDataSet

    DataTable

    DataTable

    DataRowDataRow

  • 8/3/2019 Intro to Ado.net

    33/37

    Using DataTablesWith a DataTable we can Insert, modify and update

    Search Apply views

    Compare

    Clear

    Clone and Copy

  • 8/3/2019 Intro to Ado.net

    34/37

    DataRelations New to ADO.Net

    Tables within a DataSet can now haverelationships, with integrity.

    Supports cascading updates and deletes.

  • 8/3/2019 Intro to Ado.net

    35/37

    DataViews Like a SQL view

    Single, or multiple tables

    Normally used with GUI applications via DataBinding.

  • 8/3/2019 Intro to Ado.net

    36/37

    References ADO.Net Programmers Reference

    Bilbija, Dickenson et al.Wrox Press

    http://oberon.idunno.org/sql/

    My email :[email protected]

  • 8/3/2019 Intro to Ado.net

    37/37

    Muhammad Danish Iqbal Kh


Recommended