Several practical methods for connecting pages to databases in ASP

Source: Internet
Author: User

Several practical methods for connecting pages to databases in ASP

This article discusses several practical methods for connecting pages and databases in ASP. Finally, an example of using the functions provided in this article to access the SQL Server database is given, it is of great practical significance for the development of automated network systems.

Internet function modules, such as message boards, discussion groups, online voting, online surveys, and chat rooms, can be seen everywhere on the Internet. Various office automation network systems are also running in small LAN networks of many organizations, these programs are implemented by combining ASP (Active Server Page) with database technology. Establishing a connection between the page and the database source is a step to access the database and the most important step.

In ASP scripts, you can use three practical methods to connect to a database: establish a connection through odbc dsn, establish a connection through oledb, and establish a connection through driver.

1. Establish a connection through ODBC DSN

To use the ODBC data source, you must first set the data source in ODBC on the control panel, and then write a script to establish a connection with the database source.

1. Create an ODBC DSN

You can open the control panel in the Start Menu of Windows to create a DSN-based file. Double-click "ODBC", select "system DSN", click "add", select the database driver, and click "Next ". Follow the instructions below to configure the DSN for your database software. Common Database software include Microsoft Access and SQL Server. Here we take the SQL Server database as an example.

Configure the database system DSN of SQL Server: if the database resides on a remote Server, contact the Server administrator to obtain additional configuration information. The following procedure uses the default ODBC settings of SQL Server, it may not apply to your hardware configuration. In the "Create new data source" dialog box, select "SQL Server" from the list box and click "Next ". Enter the name of The DSN File and click "Next ". Click "finish" to create a data source. Enter the name, logon ID, and password of the server that runs the SQL service program. In the "create SQL Server new data source" dialog box, type the name of the Server that contains the SQL Server database in the "Server" list box, and then click "Next ". Select the login ID verification method. If you want to select SQL Server verification, enter a logon ID and password, and then click "Next ". In the "create SQL Server new data source" dialog box, set the default database, stored procedure settings driver and ANSI identity, and then click "Next ". (For details, click "help ".) In the dialog box (also named "create SQL Server's new data source"), select a character conversion method and click "Next ". (For details, click "help ".) In the next dialog box (also named "create SQL Server's new data source"), select Logon Settings. Note that in typical cases, you can only use logs to debug database access problems. In the "ODBC Microsoft SQL Server installer" dialog box, click "Test Data Source ". If the DSN is created correctly, the "test result" dialog box indicates that the test is successful.

2. write scripts and establish a connection with the database source.

ADO (ActiveX Data Objects) provides a Connection object that can be used to establish and manage connections between applications and ODBC databases. Connection objects have various attributes and Methods. You can use them to open and close database connections. To write a database Connection script, first create an instance of the Connection object and then open the database Connection:

<%

Set conn = server. createobject ("adodb. connection ")

Conn. open "DSN = ODBC name; Database = Database name; UID =; PWD = ;"

%>

2. Establish a connection through oledb

Using the oledb method to establish a connection between the page and the database, you do not need to create an odbc dsn data source. Writing the following script to establish a connection with the data source is a simple and easy-to-use method.

<%

Set conn = server. createobject ("adodb. connection ")

Conn. open "Provider = SQLOLEDB; server = (SQL server name or IP address); database = database name; uid =; pwd ="

%>

3. Establish a connection through driver

You do not need to create an odbc dsn data source to establish a connection between the page and the database through the driver, but you must know the actual database file path or data source name (for example, SQLserver database ).

<%

Set conn = server. createobject ("adodb. connection ")

Conn. open "driver = {SQL server}; server = (SQL server name or IP address); database = database name; uid =; pwd =" %>

4. instance (LOGIN System)

Whether you enter the forum to express your opinion or enter the automated management system, you must fill in the login information. The system checks whether the user's registration information exists in its own database, you can log on successfully. The implementation of this login check process requires the connection between the background database and the front-end page.

Code implementation: the connection script can be written into a single page named conn. asp, which can be referenced as needed. You can also directly write it on the logon check page.

Conn. asp source code:

<%

Set conn = server. createobject ("adodb. connection ")

Conn. open "dsn = test; database = test; uid = czx; pwd = czx123 ;"

%>

Login_check.asp (logon check page)

<! -- # Include file = "conn. asp" -->

<%

Dim name, pwd

Name = trim (Request ("name "))

Pwd = trim (Request ("pwd "))

If Request ("B1") = "login" then

Set rs = server. createobject ("adodb. recordset ")

SQL = "select * from member where loginname = '" & name &"'"

Rs. open SQL, conn, 1, 1

If rs. eof and rs. bof then

Response. Write "You do not have permission to enter. Please register a new user first. "

Else

If rs ("password") = pwd then

Session ("memid") = rs ("id ")

Rs. close

Set rs = nothing

Response. Redirect "index. asp"

End if

End if

End if

Conn. close

Set conn = nothing

%>

V. Conclusion

This article introduces several practical methods for connecting to the database, which can be used to easily connect pages to the SQL database. The connection between pages and databases is the foundation of the automation system. In fact, the effectiveness of the application is very important. The method described in this article is a summary of the author's extensive practical experience. In fact, the effectiveness of the application has been verified.

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.