connecting Databases with ASP

Source: Internet
Author: User
Tags contains dsn include odbc access database root directory
Connection Database Original Author: Alan Saldanha

Microsoft's ASP plus IIS3.0 can provide a flexible way to build Web sites, intranet applications, and easily access ODBC-compliant databases.

In this article, we will learn the basic operations of the database, such as creating, adding records, deleting records, editing records, and so on.

The dynamic home page built using ASP has nothing to do with browsers, that is, you can browse ASP's pages in either browser, using JScript and VBScript plus some other ASP components, and you can use ASP in your intranet applications. Using ASP means not only that you can read the databases of access and SQL Server, but also that you can read other ODBC-compliant databases. Using ASP will be much faster than writing CGI, and for those who are accustomed to Visual Basic, ASPs will be the best choice.

Others, such as chili! ASP functionality is similar to ASP, can be used on many WWW platforms, even some UNIX servers (such as Chongqing Sun City, Linux, ASP scripts are used in Perl language.) .

I didn't use the debugger when I was debugging ASP code, and I found that most of the errors were caused by my spelling mistakes, or when I was embedding variables in HTML. Defeat #ü voluntarily salvation tml Reference ASP variable value, or output, I have explained in the first article. .

 

 

The code provided here is just an example and cannot be used as an application. If you try to delete a record or edit a record when there are no records, the program does not handle the situation. This example is built on the NT4.0, the database with Ms Access 7, you must have 32-bit ODBC driver. (If you have IIS3 installed.) 0 or access, that nature is already there,

The following example contains the basic operations of the database, but these operations may be used by everyone who accesses the database with ASP. You can add records to the database, edit or delete records.

 

Figure I: Database Microsoft Access

 

Database

I use Access database to build a database User.mdb

He contains two tables: Tbluser,tbluseraccess.

Tbluser, which contains some information about the user, has the following fields:-user name, user login name, user password, logon time and access level (USERNAME, Userlogin, UserPassword, Userentrydate, and Accessid). Accessid, the user level points to the Accessid field in the table tbluseraccess. Table tbluseraccess is used to define access levels-ACCESSDESC

 

Dsn

Once you have built the database, you need to create a DSN portal for the database, not to name him: UserDB1. Follow the steps below: (for DSN, I'll introduce you at the end of this article.)

Click Tender  紨 to select the Settings Control Panel.

Double-click the icon "32-bit ODBC" to bring up a dialog box with the tab "System DSN" selected

Click Add to add a DSN entry and select Microsoft Access Drive. Confirm.

Enter "UserDB1" in the "Data Source Name" field and click "Select" To choose the database, and you can select Users.mdb by clicking Browse. You can put the database in any location, such as creating a directory to put it: c:\inetsrv \wwwroot\users\users.mdb

 

Figure II: Connecting the database, method one


Connection Database Method One

ASP provides two ways to access the database. In the first method, you must first establish a connection, after which you can load any legitimate SQL statements. Finally, close the connection. Some code you can use directly. In figure II, with laid serDB1 斪 鯠 sn, you can use execute to load any legitimate SQL statements after you establish a connection. The data is then referenced in a loop. It must be noted that at the end of the note you must remember to close the connection.

 

 

Figure Three: Connection Database method Two

SUB Session_OnStart

'----Open ADO connection to database

Conn.Open "UsersDB1", "Userdblogin", "Userdbpassword"

End SUB

 

Figure IV: String used for connection in file Global.asa

Conn.Open "UserDB1", "Userdblogin", "Userdbpassword"

Connection Database Method Two:

Another approach is to establish a connection in the user's session. When the session ends, close the connection. This will use file Global.asa. Each ASP-based intranet application can have a Global.asa, which should be stored in the root directory of the application. Four event handlers can be defined in the Global.asa file, four events are:-Application-start, Session-start, Application-end, and Session-end. You can open the database connection in the event Session-start and close the connection in the event session-end.

In Figure four, using the username rock serdblogin ", the password" Userdbpassword "establishes a connection to the DSN" UserDB1 ". The connection "Conn" can be used throughout the session.

You can see examples default.asp and file Global.asa.

 

 

Figure five: Gets the value of the form element.

Action = Request.Form ("action")

 

Get information from a form

In an ASP based application, you can Chengdai the ASP example into a special file, and then include it with "include" where needed. Figure V Gets the value of the form element "Action"

Figure VI: Generating control bars



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.