一个关于在VB中将Recordset 读出的内容放到一个数组变量中的问题!!急!!求助!!

xivi 2003-09-20 05:11:07
请问,在VB中如何将recordset 中的内容读到一个数组变量中。数组要求是可变的,因为随着我的表中的内容不断多,就不可能用一个固定的数组接受全部的recordset内容。是我下面的这样吗??请帮我改正一下!!
==========================================================================
private cnn as new adodb.connection
private rs as new adodb.recordset

private sub form_load()
dim strCnn as string
dim Sql as string
dim getRS

strcnn="provider=microsoft.jet.oledb.4.0;data source=c:\db1.mdb"
Sql="select * from myTable"

cnn.open strCnn
rs.LockType = adLockOptimistic
rs.CursorType = adOpenKeyset
rs.Open Sql, cnn

getRS=rs.GetRows


...全文
621 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoki 2003-09-22
  • 打赏
  • 举报
回复

Private Sub Form_Load()
Dim nodX As Node
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim i As Integer

Set nodX = TreeView1.Nodes.Add(, , "M", "Default") 'set the main node

'connect the db
con.Open "microsoft.jet.oledb.4.0;user id=admin;data source=d:\scanfile.mdb"
rs.Open "select doc_name from doc", con, adOpenKeyset, adLockOptimistic
For i = 1 To rs.RecordCount
Set nodX = TreeView1.Nodes.Add("M", tvwChild, "C" & i, rs!doc_name)
rs.MoveNext
Next i
rs.Close
Set rs = Nothing
con.Close
Set con = Nothing
End Sub
xivi 2003-09-21
  • 打赏
  • 举报
回复
让我试一下,其实用 recordset.GetRows 将结果赋到一个不是数组的变量中,不是会将这个不是数组的变量变成一个二维数组吗??
northwolves 2003-09-21
  • 打赏
  • 举报
回复
不必往数组中存,非要存,可以存在集合中,处理更方便
northwolves 2003-09-21
  • 打赏
  • 举报
回复
这样就可以了:


If rs.BOF Then exit sub
rs.MoveFirst
do while not rs.eof
Set nodX = TreeView1.Nodes.Add("M", tvwChild, "ID &rs("ID")", rs("doc_name"))
rs.MoveNext
LOOP
xivi 2003-09-21
  • 打赏
  • 举报
回复
为什么不能由0 开始呢???
ningkang 2003-09-21
  • 打赏
  • 举报
回复
实时错误 ‘35602’
集合中的关键字不维一。应该是TreeView出错,请看MSDN中关于TreeView的帮助
Set nodX = TreeView1.Nodes.Add("M", tvwChild, "C", arryStr(i))
"M", "C", 不对

dengyiwolf 2003-09-21
  • 打赏
  • 举报
回复
For i = 0 To rs.RecordCount 这里也错了吧
应改成
For i = 1 To rs.RecordCount 或者 For i = 0 To rs.RecordCount-1
xivi 2003-09-21
  • 打赏
  • 举报
回复
那应该怎样改呢???请说明一下吧!!
xivi 2003-09-21
  • 打赏
  • 举报
回复
我的程序是这样的,我需要将一个表中的某一个字段的内容读到一个数组中,然后用来建一个Treeview的。但我不知道,为什么总是说我错
错误是:

实时错误 ‘35602’
集合中的关键字不维一。

但是就算我用primary key 字段,也是这么错。真是不知道为什么,请帮一下忙解决
=========================================
'******** 数据库中包含的字段如下 ************
' ID doc_name doc_path file_name
'********************************************



Private Sub Form_Load()
Dim nodX As Node
Dim strcnn, inRows() As String
Dim strsql As String


Dim i, j As Integer



strsql = "select * from doc"
Set nodX = TreeView1.Nodes.Add(, , "M", "Default") 'set the main node

'connect the db
con.Provider = "microsoft.jet.oledb.4.0"
con.ConnectionString = "user id=admin;data source=d:\scanfile.mdb"
con.Open
rs.LockType = adLockOptimistic
rs.CursorType = adOpenKeyset
rs.Open strsql, con


If Not rs.BOF Then
rs.MoveFirst
End If
For i = 0 To rs.RecordCount
ReDim Preserve arryStr(i)
arryStr(i) = rs("doc_name")
Set nodX = TreeView1.Nodes.Add("M", tvwChild, "C", arryStr(i))
rs.MoveNext
Next i




End Sub
pop3com2003 2003-09-20
  • 打赏
  • 举报
回复
up up
chao778899 2003-09-20
  • 打赏
  • 举报
回复
up
yoki 2003-09-20
  • 打赏
  • 举报
回复
该范例使用 GetRows 方法从 Recordset 中检索指定数目的行,并将结果数据填充到数组。在两种情况下 GetRows 方法返回的行将少于所需的数目:一种情况是因为达到了 EOF,另一种情况是因为 GetRows 试图检索已被其他用户删除的数据。仅当第二种情况发生时函数将返回 False。运行该过程需要使用 GetRowsOK 函数。

Public Sub GetRowsX()

Dim rstEmployees As ADODB.Recordset
Dim strCnn As String
Dim strMessage As String
Dim intRows As Integer
Dim avarRecords As Variant
Dim intRecord As Integer

' 使用雇员表中的姓名和受雇日期打开记录集。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set rstEmployees = New ADODB.Recordset
rstEmployees.Open "SELECT fName, lName, hire_date " & _
"FROM Employee ORDER BY lName", strCnn, , , adCmdText

Do While True
' 得到用户输入的行数。
strMessage = "Enter number of rows to retrieve."
intRows = Val(InputBox(strMessage))

If intRows <= 0 Then Exit Do

' 如 GetRowsOK 成功则打印结果,请注意是否达到文件末端。
If GetRowsOK(rstEmployees, intRows, _
avarRecords) Then
If intRows > UBound(avarRecords, 2) + 1 Then
Debug.Print "(Not enough records in " & _
"Recordset to retrieve " & intRows & _
" rows.)"
End If
Debug.Print UBound(avarRecords, 2) + 1 & _
" records found."

' 打印已检索的数据。
For intRecord = 0 To UBound(avarRecords, 2)
Debug.Print " " & _
avarRecords(0, intRecord) & " " & _
avarRecords(1, intRecord) & ", " & _
avarRecords(2, intRecord)
Next intRecord
Else
' 假定 GetRows 错误源于其他用户对数据的更改,
' 使用 Requery 刷新 Recordset 并重新开始。
If MsgBox("GetRows failed--retry?", _
vbYesNo) = vbYes Then
rstEmployees.Requery
Else
Debug.Print "GetRows failed!"
Exit Do
End If
End If

' 由于使用 GetRows 使当前记录指针指向访问过的最后一个记录,
' 所以,在循环回到另一次搜索前将记录指针移回 Recordset 的开始。
rstEmployees.MoveFirst
Loop

rstEmployees.Close

End Sub

Public Function GetRowsOK(rstTemp As ADODB.Recordset, _
intNumber As Integer, avarData As Variant) As Boolean

' 将 GetRows 方法的结果保存在数组中。
avarData = rstTemp.GetRows(intNumber)
' 仅当返回的行数少于所需的行数而非由于到达了 Recordset 末端时才返回 False。
If intNumber > UBound(avarData, 2) + 1 And _
Not rstTemp.EOF Then
GetRowsOK = False
Else
GetRowsOK = True
End If

End Function

yoki 2003-09-20
  • 打赏
  • 举报
回复
private cnn as new adodb.connection
private rs as new adodb.recordset
dim arryStr() as string

private sub form_load()
dim strCnn as string
dim Sql as string
dim i as integer

strcnn="provider=microsoft.jet.oledb.4.0;data source=c:\db1.mdb"
Sql="select * from myTable"
cnn.open strCnn
rs.LockType = adLockOptimistic
rs.CursorType = adOpenKeyset
rs.Open Sql, cnn
for i=1 to rs.recordcount
ReDim Preserve arryStr(i)
arryStr(i)=rs!field1
rs.movenext
next i

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧