数据集搜索问题
ADO数据集,我需要从头搜索某条符合条件的数据,做相关的处理,再继续搜索下一条符合条件的数据直至搜索完整个数据集。 问题点数:30、回复次数:5Top
1 楼houfuzhu(**追^_^梦**)回复于 2003-12-04 08:44:59 得分 10
不知你用的是什么数据库,暂且就写
如下:
dim cn as new adodb.connnection
dim rs as new adodb.recordset
dim sql as string
sql="select * from ..."
cn.conncetionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..."
rs.open sql,cn
rs.movefirst
while not rs.eof
if rs.fileds(...).value="..." then
...
endif
rs.movenext
wend
试试看,行不Top
2 楼victorycyz(--)回复于 2003-12-04 09:06:17 得分 0
建议用filterTop
3 楼SoHo_Andy(冰)回复于 2003-12-04 09:10:39 得分 10
两种方法,一种是使用楼上的单条循环,缺点是在循环当中没有占用资源
如果你自己需要处理的动作时间较长,就不适合使用
一种是定义两个记录集,一个用于打开的记录数量较多的数据集,一个用于过滤符合
你条件的记录集,实例代码如下
来自ADO帮助
This example uses the Filter property to open a new Recordset based on a specified condition applied to an existing Recordset. It uses the RecordCount property to show the number of records in the two Recordsets. The FilterField function is required for this procedure to run.
Public Sub FilterX()
Dim rstPublishers As ADODB.Recordset
Dim rstPublishersCountry As ADODB.Recordset
Dim strCnn As String
Dim intPublisherCount As Integer
Dim strCountry As String
Dim strMessage As String
' Open recordset with data from Publishers table.
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set rstPublishers = New ADODB.Recordset
rstPublishers.CursorType = adOpenStatic
rstPublishers.Open "publishers", strCnn, , , adCmdTable
' Populate the Recordset.
intPublisherCount = rstPublishers.RecordCount
' Get user input.
strCountry = Trim(InputBox( _
"Enter a country to filter on:"))
If strCountry <> "" Then
' Open a filtered Recordset object.
Set rstPublishersCountry = _
FilterField(rstPublishers, "Country", strCountry)
If rstPublishersCountry.RecordCount = 0 Then
MsgBox "No publishers from that country."
Else
' Print number of records for the original
' Recordset object and the filtered Recordset
' object.
strMessage = "Orders in original recordset: " & _
vbCr & intPublisherCount & vbCr & _
"Orders in filtered recordset (Country = '" & _
strCountry & "'): " & vbCr & _
rstPublishersCountry.RecordCount
MsgBox strMessage
End If
rstPublishersCountry.Close
End If
End Sub
Public Function FilterField(rstTemp As ADODB.Recordset, _
strField As String, strFilter As String) As ADODB.Recordset
' Set a filter on the specified Recordset object and then
' open a new Recordset object.
rstTemp.Filter = strField & " = '" & strFilter & "'"
Set FilterField = rstTemp
End Function
Note When you know the data you want to select, it's usually more efficient to open a Recordset with an SQL statement. This example shows how you can create just one Recordset and obtain records from a particular country.
Public Sub FilterX2()
Dim rstPublishers As ADODB.Recordset
Dim strCnn As String
' Open recordset with data from Publishers table.
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set rstPublishers = New ADODB.Recordset
rstPublishers.CursorType = adOpenStatic
rstPublishers.Open "SELECT * FROM publishers " & _
"WHERE Country = 'USA'", strCnn, , , adCmdText
' Print current data in recordset.
rstPublishers.MoveFirst
Do While Not rstPublishers.EOF
Debug.Print rstPublishers!pub_name & ", " & _
rstPublishers!country
rstPublishers.MoveNext
Loop
rstPublishers.Close
End Sub
Top
4 楼caojianan()回复于 2003-12-04 12:06:38 得分 0
做两个记录集。
做临时表。
在数据库端直接形成一个记录集。
“SELECT xx FROM (SELECT * FROM T1)AS T1 WHERE T1。XX IN (SELECT * FROM T2) ”Top
5 楼jpinglee(想去海边)回复于 2003-12-04 20:43:15 得分 10
dim cn as new adodb.connnection
dim rs as new adodb.recordset
dim sql as string
sql="select * from ..."
cn.conncetionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..."
rs.open sql,cn
rs.movefirst
while not rs.eof
修改处理. 最好在此调用一窗体,专门用来修改每一条记录
rs.movenext
wend
Top




