Open VB6 and create ActiveX DLL
2. Add Microsoft Active Server Pages object library selection to the project reference
3. Add Code As follows:
Copy code The Code is as follows: 'Code start
'Declaration part
Private myscriptingcontext as scriptingcontext
Private myapplication as application
Private myrequest as request
Private myresponse as response
Private myserver as server
Private mysession as session
'The following defines public functions (accessing ASP objects in VB, in VB, myapplication can be equivalent to application in ASP. myrequest is equivalent to request in ASP. myresponse is equivalent to response in ASP. myserver is equivalent to server in ASP. mysession is equivalent to session in ASP. use)
Public sub onstartpage (passedscriptingcontext as scriptingcontext)
Set myscriptingcontext = passedscriptingcontext
Set myapplication = myscriptingcontext. Application
Set myrequest = myscriptingcontext. Request
Set myresponse = myscriptingcontext. Response
Set myserver = myscriptingcontext. Server
Set mysession = myscriptingcontext. Session
End sub
Public sub onendpage ()
Set myscriptingcontext = nothing
Set myapplication = nothing
Set myrequest = nothing
Set myresponse = nothing
Set myserver = nothing
Set mysession = nothing
End sub
'Create a user-defined function sayhello
Public sub sayhello ()
Myresponse. Write ("Hello World ")
End sub
'Code end
4. Change the class name to helloworld and change the project name to testvbcode.
5. Generate the testvbcode. dll file and run the register component command regsvr32 path \ testvbcode. dll on Windows to register the file. (The uninstall component command is regsvr32/u path \ testvbcode. dll)
6. Create the test. asp file. The Code is as follows:Copy codeThe Code is as follows: <%
'Vb self-built function call format
'Set object name = server. Createobject ("project name. Class Name ")
'Object name. self-built function name
Set mytestobj = server. Createobject ("testvbcode. helloworld ")
Mytestobj. sayhello
%>
7. The result of running the test. asp file is as follows:
Hello World
========================================================== ==============================
For more complex applications, you can use this instance to scale out.
For example:Copy codeThe Code is as follows: public sub connstr2 ()
Set conn = myserver. Createobject ("ADODB. Connection ")
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & myserver. mappath ("codata. mdb ")
Set rs = conn. Execute ("select * From News ")
Do while not Rs. EOF
Myresponse. Write (RS ("news_title") & "<br> ")
Rs. movenext
Loop
Rs. Close
Set conn = nothing
End sub
This is encapsulated by the database connection code. Of course, ADO reference should be added here.