组件能否返回对象?

fxbird 2003-05-11 09:54:10
我用VB编了个组件,只有一个函数, getdb(),目的是打开一个名为“ce”的数据源,可在ASP里创建它的实例并赋值给一个连接对象时,总报错说运行时错误,函数是否不能返回对象呢?
该函数代码如下:
Public Function getdb()
Dim conn As New ADODB.Connection
conn.Open "ce"
getdb = conn
End Function

asp代码如下:
set obj=server.createobject("getdb.class1")
set cnn=obj.getdb
getdb.class1是我定义的组件名。请此方面的高手指教一下。
...全文
27 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Ringfo 2003-05-12
Public Function getdb() as object
.....
End Function
回复
online 2003-05-12
其实,大家在书写asp程序时,最担心的可能就是数据库的连接信息,会被一些人看到,进行一些破坏。下面我们用vb6.0来做一个动态链接库(.dll)来隐藏数据库的连接信息。
  启动vb6.0,新建-->Active dll工程。单击"工程"-->引用,选择"microsoft active server pages object library"
和"microsoft activeX data objects 2.1 library"两项。将类模块的名称改为dcss.将工程的名称改为yygwy.保存工程文件yygwy.vbp和类文件dcss.cls。
在dcss.cls中写入:
Private myscriptingcontext As ScriptingContext
Private myapplication As Application
Private myrequest As Request
Private myresponse As Response
Private myserver As Server
Private mysession As Session

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

'以上语句是必须的。
'定义两个公有函数

Public Function rsresult(strsql As String) As Recordset
Dim mycnn As Connection
Dim myset As Recordset
Dim strconnstring As String
'strconnstring = "provider=sqloledb.1;
password=;" & "user id=sa;" & "initial catalog=vlog;" & "data source=hpe60;
connect timeout=15"
strconnstring = "driver={sql server};server=yang;uid=sa;pwd=;
database=dcss"
'mycnn.ConnectionString = strconnstring
mycnn.Open strconnstring
myset.ActiveConnection = mycnn
myset.Open strsql, mycnn, 3, adCmdText
Set rsresult = myset
End Function

Public Function datasource() As Variant
datasource = "driver={sql server};server=yang;uid=sa;pwd=; database=dcss"
End Function

编译生成dcss.dll文件。注册regsvr32 路径\dcss.dll。
用visual interdev打开global.asa文件.当然了,你也可以在其它文件中使用。
set dcss=server.CreateObject("yygwy.dcss")
oconn=dcss.datasource()
application("strconn")=oconn

在其它的页面中如下调用即可:
set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open application("strconn")
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2003-05-11 09:54
社区公告
暂无公告