小弟初学vb数据库编程,在学习类的过程中遇到一个问题。
小弟想做一个类,把几种常用的带参数调用存储过程的方法做
成该类的事件,在程序中直接使用。
但是遇到一个问题,就是才类中通过调用存储过程返回的
记录集如何绑定到需要的控件上(主要是datagrid)?
如果大家有比较好的类似例子能介绍给我吗?
谢谢各位!
小弟想做一个类,把几种常用的带参数调用存储过程的方法做
成该类的事件,在程序中直接使用。
但是遇到一个问题,就是才类中通过调用存储过程返回的
记录集如何绑定到需要的控件上(主要是datagrid)?
如果大家有比较好的类似例子能介绍给我吗?
谢谢各位!
dim rs as new adodb.recordset
cn.connectionString="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=WCY"
cn.open
rs.CursorLocation = adUseClient
rs.ActiveConnection = cn
rs.Open "select * from Employees",cn,adOpenDynamic ,adLockOptimistic
set datagrid1.datasource=rs
我是通过类进行查询的
例如 类名 myclass
有一个事件exect1
事件内容是
Public Sub exect1(ByVal Text1 As String, ByVal Text2 As String, ByVal corusetext As String)
Dim lcsql As String
Dim rs As New ADODB.Recordset
exetext1 = Text1
exetext2 = Text2
coruse = corusetext
'pinlei1是调用的存储过程名
lcsql = "exec pinlei1 '" & exetext1 & " ' , '" & exetext2 & "'"
rs.Open lcsql, cnn, 3, 3, adAsyncFetch + adAsyncExecute
我在程序中通过myclass.exect1 "参数1","参数2"调用类进行查询
我想问的是如何把这个事件得到的结果集合
绑定到控件上
谢谢
Dim lcsql As String
Dim rs As New ADODB.Recordset
exetext1 = Text1
exetext2 = Text2
coruse = corusetext
'pinlei1是调用的存储过程名
lcsql = "exec pinlei1 '" & exetext1 & " ' , '" & exetext2 & "'"
rs.Open lcsql, cnn, 3, 3, adAsyncFetch + adAsyncExecute
set exect1=rs
end function不行吗?
set datagrid1.datasource = exect1(text1,Text2,corusetext)
对了得先建立这个类,比如你的类的名字叫Class1
那么你应该是:
Dim obj1 as new Class1
set datagrid1.datasource = obj1.exect1(text1,Text2,corusetext)
接收返回的记录集然后进行绑定?
但是我想对接收了返回的记录集的记录集进行
处理的时候系统给我这个提示
connection is busy with results for another hstmt
程序代码如下
Dim pinlei As New ADODB.Recordset
Dim my As New myClass
Dim retail As Double '销售总额
Dim cost As Double '成本
Dim gain As Double '毛利
Dim rate As Double '毛利率
Dim drop As Double '失窃金额
Dim dropcost As Double '失窃成本
Dim sumretail As Double
Dim sumcost As Double
Dim sumgain As Double
Dim sumrate As Double
Dim sumdrop As Double
Dim sumdropcost As Double
Dim top1 As String
Dim nex As String
startime1 = strtime.Text
endtime1 = endtime.Text
Set pinlei = New ADODB.Recordset
strsql = "select * from category"
pinlei.Open strsql, cnn, 3, 3
strsql = ""
Set biaotemp = New ADODB.Recordset
biaotemp.CursorLocation = 3
biaotemp.CursorLocation = adUseClient
biaotemp.CursorType = adOpenStatic
biaotemp.LockType = adLockReadOnly
biaotemp.CacheSize = 1000
Set biaotemp = my.exect1(startime1, endtime1, 1)
Do
'时时检测记录集状态
If biaotemp.State And adStateOpen Then
MsgBox ("数据采集完毕!")
biaotemp.MoveFirst
top1 = biaotemp!品类
Do While biaotemp.EOF = False
nex = biaotemp!品类
If top1 = nex Then
retail = retail + biaotemp!销售总额
cost = cost + biaotemp!销售成本
gain = retail - cost
If retail = 0 Then
rate = 0
Else
rate = gain / retail
End If
drop = drop + biaotemp!报损总额
dropcost = dropcost + biaotemp!报损成本
Else
———————————————————
| pinlei.MoveFirst“这一步就报错了”|
———————————————————
With pinlei
.Find ("编码='" & top1 & "'")
End With
With rs
rs.AddNew
rs!品类 = top1
rs!名称 = pinlei!类别
rs!销售总额 = retail
rs!成本合计 = cost
rs!毛利 = gain
rs!毛利率 = rate
rs!报损总额 = drop
rs!报损成本 = dropcost
End With
sumretail = sumretail + retail
sumcost = sumcost + cost
sumdrop = sumdrop + drop
sumdropcost = sumdropcost + dropcost
retail = 0
cost = 0
gain = 0
rate = 0
drop = 0
dropcost = 0
biaotemp.MovePrevious
End If
top1 = nex
biaotemp.MoveNext
Loop
pinlei.MoveFirst
With pinlei
.Find ("编码='" & top1 & "'")
End With
With rs
rs.AddNew
rs!品类 = top1
rs!名称 = pinlei!类别
rs!销售总额 = retail
rs!成本合计 = cost
rs!毛利 = gain
rs!毛利率 = rate
rs!报损总额 = drop
rs!报损成本 = dropcost
End With
sumretail = sumretail + retail
sumcost = sumcost + cost
sumdrop = sumdrop + drop
sumdropcost = sumdropcost + dropcost
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
sumgain = sumretail - sumcost
If sumretail = 0 Then
sumrate = 0
Else
sumrate = sumgain / sumretail
End If
Text1.Text = sumretail
Text2.Text = sumcost
Text3.Text = sumgain
Text4.Text = sumrate
Text5.Text = sumdrop
Text6.Text = sumdropcost
Unload planform
Command1.Visible = True
Command2.Visible = True
Exit Do
Else
planform.Show
End If
DoEvents
Loop
End Sub