12325_ASP-ADO

Embed Size (px)

Citation preview

  • 8/3/2019 12325_ASP-ADO

    1/22

    ADO

  • 8/3/2019 12325_ASP-ADO

    2/22

    Accessing a Database from an ASP Page

    y The common way to access a database from inside an ASP page is

    to:

    y Create an ADO connection to a database

    y Open the database connection

    y Create an ADO recordset

    y Open the recordset

    y Extract the data you need from the recordset

    y

    Close the recordsety Close the connection

  • 8/3/2019 12325_ASP-ADO

    3/22

    What is ADO?y ADO can be used to access databases from your web pages.

    y ADO is a Microsoft technology

    y ADO stands for ActiveXDataObjects

    y ADO is a Microsoft Active-X componenty ADO is automatically installed with Microsoft IIS

    y ADO is a programming interface to access data in a database

  • 8/3/2019 12325_ASP-ADO

    4/22

    ADO Database Connectiony Before a database can be accessed from a web page, a database

    connection has to be established.

    y Create an ODBCDatabase Connection

    y If you have an ODBC database called "northwind" you can

    connect to the database with the following ASP code:y

    y With an ODBC connection, you can connect to any database, on

    any computer in your network, as long as an ODBC connection is

    available.

  • 8/3/2019 12325_ASP-ADO

    5/22

    ADO Recordset

    y Create an ADO Table Recordset

    y After an ADO Database Connection has been created, it is possible

    to create an ADO Recordset.

    y

    Suppose we have a database named "Northwind", we can getaccess to the "Customers" table inside the database with the

    following lines:

    y

  • 8/3/2019 12325_ASP-ADO

    6/22

    ADO Recordset

    y Create an ADO SQL Recordset

    y We can also get access to the data in the "Customers" table

    using SQL:

    y

  • 8/3/2019 12325_ASP-ADO

    7/22

    ADO Recordset

    y Extract Data from the Recordset

    y After a recordset is opened, we can extract data from recordset.y Suppose we have a database named "Northwind", we can get

    access to the "Customers" table inside the database with the

    following lines:

    y

  • 8/3/2019 12325_ASP-ADO

    8/22

  • 8/3/2019 12325_ASP-ADO

    9/22

    Cont

    ydo until rs.EOFfor each x in rs.Fields

    Response.Write(x.name)

    Response.Write(" = ")

    Response.Write(x.value & "
    ")next

    Response.Write("
    ")

    rs.MoveNext

    looprs.close

    conn.close

    %>

  • 8/3/2019 12325_ASP-ADO

    10/22

    Output Result

    y CustomerID = 12345

    CompanyName = ABC DEF

    ContactName = MNO

    Address = Obere Str. 57City = DELHI

    PostalCode = 122365

    Country = INDIA

  • 8/3/2019 12325_ASP-ADO

    11/22

    Demo_add.asp

    y

  • 8/3/2019 12325_ASP-ADO

    12/22

    Demo_add.asp

    y sql="INSERT INTO customers

    (customerID,companyname,"

    sql=sql & "contactname,address,city,postalcode,country)"

    sql=sql & " VALUES "

    sql=sql & "('" & Request.Form("custid") & "',"

    sql=sql & "'" & Request.Form("compname") & "',"

    sql=sql & "'" & Request.Form("contname") & "',"

  • 8/3/2019 12325_ASP-ADO

    13/22

    Demo_add.asp

    y on error resume next

    conn.Execute sql,recaffected

    if err0 then

    Response.Write("No update permissions!")

    else

    Response.Write("" & recaffected & " record

    added")

    end if

    conn.close%>

  • 8/3/2019 12325_ASP-ADO

    14/22

    Updating Recordy sql="UPDATE customers SET "

    y sql=sql & "companyname='"

    &Request.Form("companyname") & "',

    y sql=sql & "contactname='"

    &Request.Form("contactname") & "',"

    y sql=sql & "address='" & Request.Form("address") & "',"

    y sql=sql & "city='" & Request.Form("city") & "',"

  • 8/3/2019 12325_ASP-ADO

    15/22

    Cont

    y sql=sql & "postalcode='" & Request.Form("postalcode") &

    "',"

    sql=sql & "country='" & Request.Form("country") & "'"

    sql=sql & " WHERE customerID='" & cid & "'"

    on error resume next

    conn.Execute sql

    if err0 then

    response.write("No update permissions!")

    elseresponse.write("Record " & cid & " was updated!")

    end if

    end if

    conn.close

  • 8/3/2019 12325_ASP-ADO

    16/22

    Deleting record

    y

  • 8/3/2019 12325_ASP-ADO

    17/22

    Cont

    y if err0 then

    response.write("No update permissions!")

    else

    response.write("Record " & cid & " was deleted!")

    end if

    end if

    conn.close

    %>

  • 8/3/2019 12325_ASP-ADO

    18/22

    Recordset Object

    y The ADO Recordset object is used to hold a set of records

    from a database table. A Recordset object consist of records

    and columns.

    y

    Syntax:y set

    objRecordset=Server.CreateObject("ADODB.recordset")

    y It supports 2 types of updation:

    y Immediate updatingy Batch updating

  • 8/3/2019 12325_ASP-ADO

    19/22

    Properties

    y Absolute Page:Sets or returns a value that specifies the page

    number in the Recordset object.

    y AbsolutePosition:Sets or returns a value that specifies the

    ordinal position of the current record in the Recordset objecty Bookmark:Sets or returns a bookmark. The bookmark saves

    the position of the current record.

    y CacheSize:Sets or returns the number of records that can be

    cached.y CursorLocation:Sets or returns the location of the cursor

    service

  • 8/3/2019 12325_ASP-ADO

    20/22

    Cont

    y EOF:Returns true if the current record position is after the

    last record, otherwise false

    y Filter

    y

    Indexy MaxRecords:Sets or returns the maximum number of

    records to return to a Recordset object from a query.

    y PageCount

    y RecordCount

    y PageSize

    y Sort

    y Status

  • 8/3/2019 12325_ASP-ADO

    21/22

    Methodsy AddNew

    y Cancel

    y CancelUpdate

    y Close:Closes a Recordset

    yMove

    y MoveFirst

    y MoveLast

    y MoveNext

    y MovePrevious

    y Open

    y Save

    y

    Seek,Update,UpdateB

    atch

  • 8/3/2019 12325_ASP-ADO

    22/22

    Thanx!