Using Database ASP

Home  »  ASP  »  ASP Using Database




Using Database

     Cookies only work with clients who have set up their browsers to accept cookies, the Session Object is only good for maintaining state for the duration of a user's visit to your site, and using text files to save and retrieve data can become inefficient for large amounts of information.

    Database are specifically designed to store massive amounts of information efficiently. Many commercial databases are available, including Acess, Microsoft SQL-Server, Oracle, and Informix, to name just a few.


    It is possible to read and modify the contents of a database through and ASP page. This is an amazingly useful function of Active Server Pages.


Contents

  • What Are Relational Database?

  • Why Use Databases?

  • Working with Database Using ASP

  • Using ADO to Connect to Database and Retrieve the Contents of a Table

  • What Are Relational Database?

    The most useful feature of Active Server Pages is the capability for an ASP page to easily interact with a database.

    A database is a collection of information that can easily be queried and modified.

    When using a database, you can only do four things; retrieve data, insert data, update existing data, or delete existing data. Many popular, commercial database programs are available, such as Mircorsoft's Access, which ships with Office; Microsoft SQL-Server; Orcale; Informix; FoxPro; DB2 and many others. Because ASP is an Microsoft technology, Microsoft databases are most commoly used. However, there is no reason why you cannot interact with a non-Microsoft database such as Oracle.

    The primary purpose of a database is to store information. Although each database system may have its own nuances, each databases use tables to store information.

    A table is a two-dimensional matrix that is used to store information in a database.

    Why Use Database?

    Databases, like text files, cookies, and the Session and Application objects, have their time and place. If you need to store a lot of information over a long period of time, data-bases are clearly the way to go. An entire branch of computer science is dedicated to studying database design and how to create efficient database systems. Due to this intense research into database systems, a database is the most effective and efficient method of storing information.

    Executing a database query is efficient. The inefficiency arises when you attempt to establish a connection to a database through an ASP page.


    Five Methods of Saving Data.

    Method When to Use
    Cookies

    When you have to save small bits of information on a user-by-user basis for lengthy periods of time, use cookies.

    Session Variables

    Session variables are ideal for storing information on a user-by-user basis for the duration of the user's visit to your site.

    Application Variables

    Application variables persist information from the time the web site is started until it is shut down. Only one set of application variables exists for all users on your web site.

    Text Files

    Text files are useful for storing small bits of information that need to persist for an indefinite amount of time. Text files are efficient, but storing information on a user-by-user basis using text files is difficult. Text files work best when storing long-lasting information that is not specific to each user.

    Databases

    Databases provide the best performance for storing large, heterogeneous chunks of information. Databases are designed to simplify the process of storing and retrieving information. Connecting to a database, however, carries with it a high performance cost. Databases should only be used to store large amounts of information ovr indefinite periods of time. If you only need to store some temporary value, use cookies or session variables. If you need to store some small piece of global information, such as the Web master's email address, use include files or application variables.

    For example, if you needed to save information about the clients your company does business with , you'd want to use a database.

    Working with Database Using ASP

    Befor you create your database, you need to decide what kind of information you want to store. Imagine that you wanted to create a Web page that would sotre contact information for your friends or music and loved ones. For example, create a table named Music/Songs that containts the following columns:

    • MusicID
    • SongName
    • SingerName
    • PageLink
    • Lyrics


    To create such a table using Access 2007, start by running Access. A dialog box appears, asking whether you want to Create a new database or Open an existing file. Choose to create a Blank Access Database.




    Start by creating a new Access Database




    Enter a new Access Database [ASP-Database]

    Enter the columns of your table into this martix and each row in the matrix represents a colum in the MusicTable [ASP-Database]





    Data Types Supported by Access

    Data Type When to Use
    Text

    Use when you need to store 255 alphanumeric characters or less.

    Memo

    Use when you need to store a large amount of alphanumeric characters. The Memo data type can store up to 65,535 characters.

    Number

    Use when you need to store numeric values.

    Data/Time

    Use when you need to store data and time values. A Data/Time data type in Access can store dates between the years 100 and 9999.

    Currency

    Use when you need to store currency values. Currency data types only support up to four digits to decimal precision.

    Yes/No

    Use for columns that can only have one of two values.

    Autonumber

    Use this automatically incrementing variables to uniquely identify each row in a table.





    Enter the columns of your table into this martix and each row in the matrix represents a colum in the MusicTable [ASP-Database]

    You now have created a database with one table using Access 2007. Now that you have a database, you can query it through an ASP page. It doesn't matter what database system you decide to use, so long as it is ODBC-complaint. Although the steps to actually create the database and a table might differ, querying the data through ASP wil not be any different.

    To communicate with a database through an ASP page, you need to use the ActiveX Data Objects component (ADO). This component provides a number of objects that can be used to connect to and query a database. You want to access a database through an ASP page, you must use ADO.



    Enter the information for the MusicTable table.


    When you want to retieve information from a database, you need to use a two-step process:

    1. Establish a connection to the database.
    2. Query the database, asking the database for the information you are interested in.




    Using ADO to Connect to Database and Retrieve the Contents of a Table




    Below Figure shows the output of above asp code, when viewed through a browser.




    Share on Google Plus
      Blogger Comment
      Facebook Comment