ASP basic Lectures (next)

Source: Internet
Author: User
Tags object insert mdb database odbc sql query string access
When we need to interact with browsers, there are sometimes databases. Because only a database is possible to process information in large amounts and quickly. Using ASP means not only that you can read access and SQL Server databases, but also that you can read other databases compatible with ODBC (open database Connect, open Databases interconnection). In order to access the database in ASP, we will touch on a term--ado (active data object, active DataSet).
ADO is a way to operate a database supported by Microsoft, some similar to the DAO (data Access Object) and RDO (Remote Data Object), which was previously heard in VB. In ASP, ADO can be regarded as a server component (server Component), simpler to say, is a series of objects, the application of these powerful objects, can easily complete the complex operation of the database. What we want to learn here is the way to use these objects, the specific steps can be summed up in the following steps:
(i) Create database source name (DSN)
(ii) Create a database link (Connection)
(iii) Creating data Objects
(iv) Operational database
(v) Close data Objects and links
The following is a detailed description of each step:
First, create a data source name
With ODBC, often see DSN this noun, what it is, what is the important role? The DSN (Date source name) is the data source names. We know that ODBC is a way to access the database, as long as there is a corresponding ODBC driver in the system, any program can manipulate the driver's database through ODBC. For example, our system has access to the ODBC driver, so even if we do not have access software, you can also in our program (such as VB) in the middle of an access to the MDB database add, delete, change records. And we don't even have to know where the database is. We just write the SQL statements, and the ODBC driver helps us do everything. When we pass SQL instructions to an ODBC driver, we use DSN to tell it exactly which database it is operating on. If the database platform changes, for example, we switch to SQL Server database, as long as the structure of the table is not changed, we do not have to rewrite our program, as long as the system to reconfigure DSN. This shows that DSN is the bridge between the application and the database, to access the database through ODBC, if we have to configure a good DSN (that is, a good bridge). A DSN must contain some information:
Name of DSN: This is the name of the bridge, when the program access to the database, the system is the name, rather than the actual name of the database.
ODBC driver type: Only the driver type is indicated, and the system knows which ODBC driver to service when we manipulate the database.
Database: You must specify which database the bridge is connected to, but different database systems, the method of specifying database names is somewhat different.
This bridge is in the system, so WIN95 (NT) provides a tool to accomplish this, that is, the BIT ODBC in Control Panel, there are three categories, namely, User DSN, System DSN, File DSN. We typically use a system DSN, because this will allow everyone who operates on the system to use this DSN.
Open Windows Control Panel, find the icon "32 ODBC" (some systems are called "ODBC", "32bit ODBC"), double-click, appear (like figure I) dialog box, select the label "System DSN", click "New", select the driver for Microsoft Access MDB, click Finish, Pop Up the dialog box (figure two), output "Data Source Name" as "DSN1", click OK, click "Select Database", and select the database you want to use, such as "C:\b1.mdb", so that's OK. Later we use "DSN1" to access the database "C:\b1.mdb", and not directly with the filename "C:\b1.mdb", the advantage is that once we change the file name or storage, we do not have to modify the name of all the database in the program, Instead, just modify the configuration in the DSN.
Second, create a database link (Connection)
People who have used VB know that there are links in Dao (Connection), RDO in Odo Connection. Links are used to maintain some state information about the data being accessed, as well as the linker information. If you want to access data in an ASP file, you must first create a link to the database with the following syntax:
Set Mconn=server CreateObject ("Adobdconnection")
This statement creates a linked object Mconn, and then:
Mconn.open "DSN1", "Vsername", "Password"
This statement opens the link and uses a DSN, in this case, "DSN1". The following two parameters are the user name and password to access the database, and are optional parameters.
Note: There must be two small steps, because the creation and opening of a linked object are two different things that can only be used if you open it.
Third, create Data Objects (record Set)
The data objects in ADO usually hold the query results. The record set is the most complex object in ADO, which has many properties and methods, and can achieve many good results by using it flexibly. As in DAO, the record set holds records for a row of rows and is marked with a current record. Here's how to create:
Set record Set=mconn.execute (SQTSTR)
This statement creates and opens the object record Set, where Mcon is a previously created linked object, and Sqtstr is a string that represents a standard SQL statement, for example:
Sqlstr= "SELECT * from Tab1"
Set record Set=mconn.exe cute (SQLSTR)
After this statement is executed, all records in the table tab1 are saved in the object record set. How to reference the return result in ASP file, this article will give an example explanation at the end of article.
Iv. operation of the database
Just now we returned the query result to a data object by calling the Execute method of the linked object. We say the parameters of the Execute method are a standard SQL statement string, so we can use it to easily perform operations such as inserting, modifying, and deleting data, such as:
Sqlstr= "INSERT into Tab1 VALUES (1,2)"
Mconn.execute (SQLSTR)
/Perform insert operation
Sqlstr= "UPDATE tab1 SET field1=3"
Mconn.execute (SQLSTR)
/Perform a modification operation
V. Close data Objects and linked objects
After using the ADO object, be sure to close it because it uses a certain server resource. Closed by calling method close implementation, and then releasing it.
Record Set.close
Set Record set=nothing
/close the Created data object
Mconn.close
Set mconn=nothing
/close the created linked object
So far, we have given the entire process of accessing the database with ASP, including creating a data source-creating a link-creating a data object-manipulating the data object-close and release. In the meantime, due to the application of object-oriented thinking, it should be said that all the operations are relatively simple, users need to pay attention to the data structure, but also to know what the object is currently being manipulated, what attributes, and so on. As long as these have a sober understanding, coupled with the powerful functions of ASP, on the network to apply your own database, it is no longer difficult!
Finally, this article gives an example, this is based on the database of ASP guest Book program, the database structure is as follows:
Guestbook.mdb
Table Lyb:
XM char (MB), Ly char (255), SJ datetime
File guestbook.asp:
<title>lyb</title>
<body bgcolor= "#ccccf0" >
<p> Guest Book </p>
<form method= "POST" action= "guestbook.asp" >
<input type= "hidden" name= "task" value= "Insert" >
<p> Name: <input type= "text" size= "a" name= "XM" ></p>
<p> message:</p>
<p><textarea rows= "5" cols= "name=" "ly" ></textarea></p>
<p>&l

[1] [2] Next page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.