ASP Components Intermediate beginner and Proficient Series VII
Source: Internet
Author: User
Start the database operation.
Common Component Encapsulation
1.? Encapsulate the connection information of the database.
1>???? Returns the database connection string directly, as in the component
Public? Function? DataSource ()? As? Variant
DataSource? =? "Driver={sql?" server};server=yang;uid=sa;pwd=;? Database=northwind "
End? Function
?
ASP calls
Set Obj=server. CreateObject ("Webdb.getinfo")??
Oconn=obj.datasource ()
This shortcoming is very obvious, in the ASP file, the direct Response.Write oconn can display the database connection string, does not have the anticipated function.
?
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 calls
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, just Response.Write DataQuery.getconn.ConnectionString or will display a database connection string, you can test.
?
?
?
2.? Encapsulating components into recordsets
You can take a look at the http://blog.111cn.net/online/archive/2003/12/11/7764.aspx that you wrote some time ago.
The bad thing about this code is that the connection to the database is placed on the page and the connection is successful before the data is accessed.
?
?
Personally, it is best to:
Encapsulation to Recordset, component methods to connect the database, after the operation, timely shutdown
Try to generate the HTML code in the component and do all the encapsulation. As in the following way
Rather than a partial encapsulation.
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