7.ADO.net Data Providers

Embed Size (px)

Citation preview

  • 7/29/2019 7.ADO.net Data Providers

    1/16

    ADO.NET Data Provider

  • 7/29/2019 7.ADO.net Data Providers

    2/16

    Agenda

    Introduction to ADO.NET

    ADO.NET Architecture

    Objects in ADO.NET

    .NET Data Providers

    Connecting to Data Base (SQLServer)

    Properties and Methods of the

    SqlConnection Class

    Storing Connection Strings in theConfiguration File

    Using Command Object

    Creating Command Object

    Executing a Command That Doesn'tReturn Rows.

    Executing a Command That Returnsa Single Value.

    Parameterized Command

    Using DataReader Object

    Transactions

    Transaction Demo.

  • 7/29/2019 7.ADO.net Data Providers

    3/16

    Introduction to ADO.NET

    ADO.NET is a data-access subsystem in the Microsoft .NET Framework.

    ADO.NET was developed from ADO (ActiveX Data Objects).

    In the .NET Framework, the ADO.NET libraries appear under theSystem.Data namespace.

    Supports both connected and disconnected data access.

  • 7/29/2019 7.ADO.net Data Providers

    4/16

    ADO.NET Architecture

  • 7/29/2019 7.ADO.net Data Providers

    5/16

    Objects in ADO.NET

    Connected Objects

    Connection

    Transaction

    Command

    Parameter

    DataReader

    DataAdapter

    Disconnected Objects

    Dataset

    DataTable

    DataRow

    DataColumn

    DataView

    Constraint

  • 7/29/2019 7.ADO.net Data Providers

    6/16

    .NET Data Providers

    The .NET data provider is the managed component of choice for databasevendors to expose their data in the most effective way.

    Each database vendor should provide a .NET-compatible API that is callablefrom within managed applications.

    The Data Provider is responsible for providing and maintaining the connectionto the database.

  • 7/29/2019 7.ADO.net Data Providers

    7/16

    Connecting to Data Base (SQL Server)

    Creating Connection Object

    By creating instance ofSqlConnection Class.

    SqlConnection testConnection = new SqlConnection();

    Connection String Property

    SqlConnection testConnection= new SqlConnection(

    "Data Source=(local);Initial Catalog=Test;Integrated Security=SSPI");

    (OR)

    SqlConnection testConnection = new SqlConnection();

    string testConnectionString = "Data Source=(local);Initial Catalog=Test;Integrated

    Security=SSPI";

    testConnection.ConnectionString = testConnectionString;

  • 7/29/2019 7.ADO.net Data Providers

    8/16

    Creating and Managing Connections

    To create and manage connections, you need to:Create a connection object.

    Create a command object.

    Open the connection object.

    Execute the SQL statement in the command object.

    Close the connection object.

  • 7/29/2019 7.ADO.net Data Providers

    9/16

    Storing Connection Strings in the Configuration File

    For each connection string in the application, add a new element tothe element in the configuration file, as follows:

    The classes for accessing the configuration file are found in theSystem.Configuration namespace.

    Code below creates connection using connection string stored in configurationfile

    SqlConnection MyConnection;

    MyConnection = new SqlConnection();

    MyConnection.ConnectionString =

    ConfigurationManager.ConnectionStrings [constring].ConnectionString;

  • 7/29/2019 7.ADO.net Data Providers

    10/16

    Using Command Object

    The Command object is the heart of data processing with ADO.NET.

    The Command object wraps a SQL statement or a call to a stored procedure.

    Properties and Methods of the SqlCommand Class

  • 7/29/2019 7.ADO.net Data Providers

    11/16

    Executing a Command That Doesn't Return Rows

  • 7/29/2019 7.ADO.net Data Providers

    12/16

    Executing a Command That Returns a Single Value

  • 7/29/2019 7.ADO.net Data Providers

    13/16

    Parameterized Command

  • 7/29/2019 7.ADO.net Data Providers

    14/16

    Using DataReader Object

    The SqlDataReader class defines a lightweight yet powerful object that is usedto read information from a SQL database.

    SqlDataReader retrieves query results in a read-only, forward-only stream ofinformation and will not let you perform updates.

  • 7/29/2019 7.ADO.net Data Providers

    15/16

    Using DataReader Object

    The following code snippet illustrates the typical loop you implement to readall the records of a query:

  • 7/29/2019 7.ADO.net Data Providers

    16/16

    Thank You