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.