回 帖 发 新 帖 刷新版面

主题:[讨论]asp里调用dll里函数时传递自定义类型参数以及返回RecordSet的解决方法

1、返回RecordSet
主要注意将recordSet的cursorLocation属性设置为adUseClient(3)。
VB里Dll的代码
[code=c]
Public Function getRS(ByVal connStr As String, ByVal sqlStr As String) As ADODB.Recordset
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.CursorLocation = adUseClient
conn.Open connStr
rs.CursorLocation = adUseClient
rs.Open sqlStr, conn, adOpenKeyset, adLockOptimistic
Set getRS = rs
'在VB里测试时要注释掉以下语句(不关闭rs和conn)
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Function
[/code]
Asp里调用的代码:
[code=c]
<%
dim test
dim rs
dim i
set test=server.createObject("Test.TestReturnRecordset")
set rs=server.CreateObject("adodb.recordset")
rs.CursorLocation = 3    'adUseClient
'换成自己的connStr
set rs=test.getRS("Driver={SQL Server};UID=sa;PWD=****;database=Guan;server=127.0.0.1;", "select * from hole")

rs.MoveFirst
While Not (rs.EOF Or rs.BOF)
    For i = 0 To rs.Fields.Count - 1
        response.write rs(i) &amp; "
"
    Next
    rs.MoveNext
Wend
%>
[/code]
2、在参数中使用自定义数据类型(Type)
在asp里用Class代替Type
VB里Dll的代码
[code=c]
Option Explicit
Public Type subType
    s1 As Long
    s2 As String
End Type
Public Type TestType
    a As Long
    b As Long
    c As String
    d() As subType
End Type

Public Function getResult(ByRef t As Variant) As String    '参数类型要声明为variant类型
Dim i As Integer
    getResult = "t.a=" & t.a & ",t.b=" & t.b & ",t.c=" & t.c
    For i = 0 To UBound(t.d)
        getResult = getResult & ",t.d(" & i & ").s1=" & t.d(i).s1 & ",t.d(" & i & ").s2=" & t.d(i).s2
    Next
End Function
[/code]
Asp里调用的代码
[code=c]
<%
class subType
    dim s1
    dim s2
end class
class TestType
    dim a 
    dim b 
    dim c  
    dim d()    'as subType
    public Sub SetBound(byval nIndex )
        redim d(nIndex)
    End Sub
end Class

dim test
dim t 
dim st
set test=server.createObject("Test.TestPassType")
if test is nothing then 
    response.write "创建TestPassType对象失败" &amp; "
"
    response.End
End If
set t =new TestType
t.a=120
t.b=320
t.c="this is the test string"
set st=new subType
st.s1= 250
st.s2="this is subType.s2"
'redim t.d(0)    '提示语法错误!!!
t.SetBound 1

set t.d(0)=st
st.s1=270
st.s2="this is another subType.s2"
set t.d(1)=st
response.write test.getResult(t) &amp; "
"
response.write err.description &amp; "
"
%>
[/code]完整的代码请看附件。

回复列表 (共3个回复)

沙发

发错地方了?? 没人理...

板凳

问题呢?

3 楼

[quote]问题呢?[/quote]
没问题,就分享一下遇到的这种情况的解决方法。看大家还有什么其它更好的方法。。

我来回复

您尚未登录,请登录后再回复。点此登录或注册