日期:2014-05-17  浏览次数:20810 次

谁能帮我一个小忙
我想做一个程序,三层架构,用ASP编写登陆界面,数据库为SQL,VB编写中间层,架起ASP与数据库的桥梁,请问如何VB编写DLL,谢谢!

------解决方案--------------------
实现方法:

启动你的VB,选择ActiveX图标。这个图标可以在新建工程找到!VB会提供一个默认的工程名(project1)和类名(class1)。在动手之前请首先确认我们拥有Microsoft ActiveX Data Object 2.0 Library,它在我们的程序非常有用。从菜单中选择 "工程 ",然后在其中选择 "引用 ",就会出现 "引用 "窗口从中选择Microsoft ActiveX Data Object 2.0 Library。
现在我们有了我们自己的工程(project1)和类名(class1)。以后我们就会在ASP代码中使用它们的名字来引用这个组件。在ASP中我们就这样引用,如下:
Set ObjReference = Server.CreateObject( "ProjectName.ClassName ")
为了在类中使用ASP的方法,你必须在此类中写上初始和终止这两个函数。输入如下代码:程序清单中Class1.cls中的Private Sub Class_Initialize()和Private Sub Class_Terminate()。
将ASP代码中的关键功能制作成动态链接库(.dll),部分隐藏 ASP源代码。 例如:程序清单中的Global.bas和Class1.cls中输入的代码(代码的主要功能是检索数据库中的记录并显示出来)。
在VB的下拉菜单中选择文件→生成article.dll→选择要保存的目录。找到article.dll将其复制到系统盘system32文件夹中,最后一步注册DLL文件。在开始菜单中选择运行输入regsvr32 c:\winnt\system32\article.dll。

-------------------------------------------------
程序清单:
Global.bas中的代码:
Public objContext As ObjectContext
Public Application As ASPTypeLibrary.Application
Public Server As ASPTypeLibrary.Server
Public Session As ASPTypeLibrary.Session
Public Response As ASPTypeLibrary.Response
Public Request As ASPTypeLibrary.Request
Class1.cls中的代码:
Private Sub Class_Initialize()
On Error Resume Next
Set objContext = GetObjectContext
Set Application = objContext.Item( "Application ")
Set Server = objContext.Item( "Server ")
Set Session = objContext.Item( "Session ")
Set Request = objContext.Item( "Request ")
Set Response = objContext.Item( "Response ")
End Sub
Private Sub Class_Terminate()
On Error Resume Next
Set Application = Nothing
Set Server = Nothing
Set Session = Nothing
Set Request = Nothing
Set Response = Nothing
Set objContext = Nothing
End Sub
Public Sub AspClassInit()
On Error GoTo Err
Set conn = Server.CreateObject( "ADODB.Connection ")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source= " & Server.MapPath( "Article.mdb ")
conn.Open strcon
Set rs = Server.CreateObject( "ADODB.Recordset ")
sql = "select * from Article order by ArticleID desc "
rs.Open sql, conn, 1, 1
Response.Write " <html> " & vbCrLf
Response.Write " <head> " & vbCrLf
Response.Write " <meta http-equiv= " "Content-Type " " content= " "text/html; charset=gb2312 " "> " & vbCrLf
Response.Write " <title> 文章管理系统-CSSTUDIO </title> " & vbCrLf
Response.Write " </head> " & vbCrLf
Response.Write " <body bgcolor= " "#FFFFFF " " topmargin= " "0 " "> " & vbCrLf
Response.Write " <table width= " "100% " " border= " "0 " " cellpadding= " "2 " " cellspacing= " "0 " "> " & vbCrLf
Response.Write " <tr> " & vbCrLf
Response.Write " <td width= " "742 " " height= " "20 " "> 文章标题 </td> " & vbCrLf
Response.Write " <td width= " "90 " "> 点击 </td> " & vbCrLf
Response.Write " <td width= " "145 " "> 添加日期 </td> " & vbCrLf
Response.Write " </tr> " & vbCrLf
Response.Write "