Start the database operation.
Common Component Encapsulation
1.? Encapsulate the connection information of the database.
1>???? Return directly to the database connection string, such as
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 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 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, just Response.Write DataQuery.getconn.ConnectionString or will display a database connection string, we can test.
?
?
?
2.? Encapsulate a component to a recordset
You can take a look at the http://blog.csdn.net/online/archive/2003/12/11/7764.aspx
you wrote some time ago.
This piece of code is not a good point is that the database connection to the page to judge, after the successful connection, before starting to access data, see
?
?
personally think that the best thing to do is:
encapsulation to the Recordset, the component method to connect the database, after the operation, timely shutdown
as much as possible to generate HTML code in the component, do all encapsulation. As in the following way
rather than part of the package.