Top Banner

of 27

07-Direct Data Access

Apr 10, 2018

Download

Documents

vijithc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/8/2019 07-Direct Data Access

    1/27

    Direct Data Access, Data Binding

  • 8/8/2019 07-Direct Data Access

    2/27

    Contenty Direct Data Access

    y Data Binding

    Muzaffer DOAN - Anadolu University 2

  • 8/8/2019 07-Direct Data Access

    3/27

    Muzaffer DOAN - Anadolu University 3

  • 8/8/2019 07-Direct Data Access

    4/27

    Important Note!!!y Last week, you learned how to connect to databases

    using built-in controls.

    y Use them in your applications whenever possible.y If you sure that you need to connect to database

    manually, follow the instructions explained in thisclass.

    Muzaffer DOAN - Anadolu University 4

  • 8/8/2019 07-Direct Data Access

    5/27

    Direct Data Access Querying1. Create Connection, Command, and DataReader

    objects

    2. Use the DataReader to retrieve information from thedatabase, and display it in a control

    3. Close your connection

    4. Send the page to the user

    Muzaffer DOAN - Anadolu University 5

  • 8/8/2019 07-Direct Data Access

    6/27

    Updating, Inserting, Deleting1. Create new Connection and Command objects

    2. Execute the Commandwith the appropriate SQL

    statement

    Muzaffer DOAN - Anadolu University 6

  • 8/8/2019 07-Direct Data Access

    7/27

    Direct Data Access with ADO.NET

    Muzaffer DOAN - Anadolu University 7

  • 8/8/2019 07-Direct Data Access

    8/27

    ADO.NET Data Provider ClassesSQL Server OleDB (Access)

    Connection SqlConnection OleDbConnection

    Command SqlCommand OleDbCommandDataReader SqlDataReader OleDbDataReader

    DataAdapter SqlDataAdapter OleDbDataAdapter

    Muzaffer DOAN - Anadolu University 8

    y

    Use OracleConnection, OracleCommand, etc. forOracle data providers

    y Use OdbcConnection, OdbcCommand, etc. forODBC data providers

  • 8/8/2019 07-Direct Data Access

    9/27

    Namespace Importsy Import following namespaces for SQL Server:

    y using System.Data;

    y using System.Data.SqlClient;y Import following namespaces for Access:

    y using System.Data;

    y using System.OleDb;

    Muzaffer DOAN - Anadolu University 9

  • 8/8/2019 07-Direct Data Access

    10/27

    Connecting Access DatabaseOleDbConnection conn = new OleDbConnection();

    conn.ConnectionString =

    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Survey.mdb";

    conn.Open();

    // Database operations will be here...

    conn.Close();

    Muzaffer DOAN - Anadolu University 10

  • 8/8/2019 07-Direct Data Access

    11/27

    Connecting SQL Server ExpressSqlConnection conn = new SqlConnection();

    conn.ConnectionString = @"Data Source=.\SQLEXPRESS;

    AttachDbFilename=|DataDirectory|\Survey.mdf;Integrated Security=True;User Instance=True";

    conn.Open();

    // Database operations will be here...

    conn.Close();

    Muzaffer DOAN - Anadolu University 11

  • 8/8/2019 07-Direct Data Access

    12/27

  • 8/8/2019 07-Direct Data Access

    13/27

    Storing the Connection Stringy Write the connection string into connectionString

    section ofweb.config file:

    ...

    Muzaffer DOAN - Anadolu University 13

  • 8/8/2019 07-Direct Data Access

    14/27

    Retrieving the Connection Stringy string connectionString = WebConfigurationManager.

    ConnectionStrings["Pubs"].ConnectionString;

    Muzaffer DOAN - Anadolu University 14

  • 8/8/2019 07-Direct Data Access

    15/27

    Execute Commandy Command object has several methods starting with

    the "Execute" string:y

    ExecuteNonQuery(): Used for queries that don'treturn any records (e.g. Update, Insert, Delete queries)

    y ExecuteReader(): Used for queries that return one ormore records (e.g. Select query)

    y Ex

    ecuteScalar(): Used for queries that return one ormore records but this method returns only the firstcolumn of the first row (suitable for obtaining numberof records, maximum value of a column)

    Muzaffer DOAN - Anadolu University 15

  • 8/8/2019 07-Direct Data Access

    16/27

  • 8/8/2019 07-Direct Data Access

    17/27

    The DataReadery Create a DataReader byExecuteReader method of the

    Command object

    y

    Retrieve the record by the Read() method of theDataReader object

    y To retrieve the next record, use Read() method again

    y If next record is successfully read, the Read() method

    returns truey So, continue reading until the Read() method returns

    false

    Muzaffer DOAN - Anadolu University 17

  • 8/8/2019 07-Direct Data Access

    18/27

    The DataReaderOleDbConnection conn = new OleDbConnection(

    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=

    |DataDirectory|\Survey.mdb");

    OleDbCommand cmd = new OleDbCommand("SELECT * FROMUserInfo", conn);

    conn.Open();

    OleDbDataReader reader = cmd.ExecuteReader();

    while (reader.Read())

    {

    Label1.Text += reader["FirstName"] + "
    ";

    }

    reader.Close();

    conn.Close();Muzaffer DOAN - Anadolu University 18

  • 8/8/2019 07-Direct Data Access

    19/27

    ExecuteScalar ExampleOleDbConnection conn = new OleDbConnection(

    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=

    |DataDirectory|\Survey.mdb");

    OleDbCommand cmd = new OleDbCommand("SELECTMAX(FavoriteNumber) FROM UserInfo", conn);

    conn.Open();

    int maxfav = (int)cmd.ExecuteScalar();

    conn.Close();

    Muzaffer DOAN - Anadolu University 19

  • 8/8/2019 07-Direct Data Access

    20/27

    ExecuteNonQuery ExampleOleDbConnection conn = new OleDbConnection(

    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=

    |DataDirectory|\Survey.mdb");

    OleDbCommand cmd = new OleDbCommand("DELETE * FROMUserInfo WHERE UserID=5", conn);

    conn.Open();

    int affectedRowNumber = cmd.ExecuteNonQuery();

    conn.Close();

    Muzaffer DOAN - Anadolu University 20

  • 8/8/2019 07-Direct Data Access

    21/27

    Muzaffer DOAN - Anadolu University 21

  • 8/8/2019 07-Direct Data Access

    22/27

  • 8/8/2019 07-Direct Data Access

    23/27

    Data Bindingy The basic principle of data binding is this: you tell a

    control where to find your data and how you want itdisplayed, and the control handles the rest of thedetails.

    yASP.NET data binding works in one direction only.Information moves from a data object into a control.Then the data objects are thrown away, and the page is

    sent to the client. If the user modifies the data in adata-bound control, your program can update thecorresponding record in the database, but nothinghappens automatically.

    Muzaffer DOAN - Anadolu University 23

  • 8/8/2019 07-Direct Data Access

    24/27

    Types of ASP.NET Data Bindingy Single-Value, or "Simple", Data Binding

    y Single-value data binding allows you to take a variable, a

    property, or an expression and insert it dynamically intoa page

    y Single-value binding also helps you create templates forthe rich data controls

    y

    Repeated-Value, or "List", Bindingy Allows you to display an entire table (or just a singlefield from a table)

    Muzaffer DOAN - Anadolu University 24

  • 8/8/2019 07-Direct Data Access

    25/27

    Using Data Bindingy To use single-value binding, you must insert a data

    binding expression into the markup in the .aspx file(not the code-behind file).

    y To use repeated-value binding, you must set one ormore properties of a data control.

    y Once you specify data binding, you need to activate it.You accomplish this task by calling the DataBind()

    method of the control.yAlternatively, you can bind the whole page at once by

    calling the DataBind() method of the current Pageobject.

    Muzaffer DOAN - Anadolu University 25

  • 8/8/2019 07-Direct Data Access

    26/27

    A Simple List Binding ExampleyArrayList fruit = new ArrayList();

    y fruit.Add("Kiwi");

    y fruit.Add("Mango");y fruit.Add("Blueberry");

    y fruit.Add("Apricot");

    y fruit.Add("Banana");

    y lstItems.DataSource = fruit;

    y lstItems.DataBind(); // or

    y this.DataBind();

    Muzaffer DOAN - Anadolu University 26

  • 8/8/2019 07-Direct Data Access

    27/27

    Referencesy Beginning ASP.NET 3.5 in C# 2008: From Novice to

    Professional

    y

    Visual Studio and MSDN Help

    Muzaffer DOAN - Anadolu University 27