ASP Component intermediate entry and proficient Series 7

Source: Internet
Author: User

Start database operations.

Common component Encapsulation

1 .? Encapsulate the database connection information.

1> ???? Directly return the database connection string, for example, in the component

Public? Function? Datasource ()? As? Variant

Datasource? =? "Driver = {SQL? Server}; server = Yang; uid = sa; Pwd = ;? Database = northwind"

End? Function

?

ASP call

Set OBJ = server. Createobject ("webdb. getinfo ")??

Oconn = obj. datasource ()

This disadvantage is obvious. In the ASP file, direct response. Write oconn can display the database connection string, which does not play the expected role.

?

2> ???? Returns the ADODB. connection object.

Public Function getconn () as ADODB. Connection

Set conn = new ADODB. Connection

Conn. connectionstring = "provider = sqloledb.1; persist Security info = false; user id = sa; Password =; initial catalog = northwind; Data Source = Yang"

Conn. Open

Set getconn = Conn

End Function

?

ASP call

Dim dataquery

Set dataquery = server. Createobject ("webdbtest. getinfomation ")

Set rs = server. Createobject ("ADODB. recordset ")

?

SQL = "select * from employees"

Rs. Open SQL, dataquery. getconn, 1, 3 ???

?

Response. Write RS ("lastname ")

Response. Write dataquery. getconn. connectionstring

Set rs = nothing

?

This looks good, but response. Write dataquery. getconn. connectionstring will still display the database connection string. You can test it.

?

?

?

2 .? Encapsulate components into Record Sets

Can take a look at some time to write http://blog.csdn.net/online/archive/2003/12/11/7764.aspx

The bad thing about this code is that the connection to the database is stored on the page and the database is accessed only after the connection is successful.

Flag = dataquery. getconn ()

If flag = false then

? Response. write "the database is not linked. Please check"

? Response. End

End if

Set rs = dataquery. getemployeelist ()

......

%>

?

?

In my opinion, the best practice is:

Encapsulate the data into the record set and connect to the database in the component method. After the operation, close the data in time.

Try to generate HTML code in the component for full encapsulation. This method is as follows:

The 'list method contains page generation, connection processing, paging processing, and so on.

??? Dim objloop

??? Set objloop = server. Createobject ("webdbtest. getinfomation ")

??? Objloop. List ()

??? Set objloop = nothing

%>

Instead of partial encapsulation.

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.