Vb6 记录集中只有一行,但表中的所有行都会更新
查询检索recordcount确认的单个记录,但表中的每一行都会更新 我使用的是vb6和ms ado 2.8 Firebird版本为2.5.4.26856(x64) Firebird ODBC驱动程序2.0.3.154 这台计算机是64位windows 7 home editionVb6 记录集中只有一行,但表中的所有行都会更新,vb6,ado,firebird,Vb6,Ado,Firebird,查询检索recordcount确认的单个记录,但表中的每一行都会更新 我使用的是vb6和ms ado 2.8 Firebird版本为2.5.4.26856(x64) Firebird ODBC驱动程序2.0.3.154 这台计算机是64位windows 7 home edition Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim cs As String Dim dbPath As String dbPath
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim dbPath As String
dbPath = "c:\Parkes\Parkes.fdb"
cs = "DRIVER={Firebird/Interbase(r) Driver}; DBNAME=localhost:" & dbPath & "; UID=SYSDBA; PWD=masterkey;"
cn.ConnectionString = cs
cn.Open
Dim sQuery As String
sQuery = "select memo from clients where clientID = 10021 "
rs.Open sQuery, cn, adOpenStatic, adLockOptimistic
If rs.BOF <> True Or rs.EOF <> True Then
'putting msgbox rs.recordcount here confirms only 1 record in recordset
rs.Movefirst
rs.Fields("memo") = "blah"
rs.Update
End If
Set rs = Nothing
Set cn = Nothing
当记录集只包含一行时,我无法理解如何编辑多行
编辑:在网上读了一点,这是我对正在发生的事情的理解。
update方法似乎根据记录集中选定的列来标识要更新的记录。
因此,如果选择字段a、b、c、d并正在更新字段a,它将只更新数据库中a、b、c、d的值与记录集中的值匹配的记录。
确保只更新单个记录的最佳方法是在所选字段中包含主键。
因此,如果我按照下面的行编写查询,那么只会更新一条记录,因为clientID列包含唯一的值
sQuery = "select memo, clientID from clients where clientID = 10021 "
考虑这一点是有道理的,但根据我的经验,我最初编写查询的方式在其他数据库中似乎运行良好,还是我错了?我测试了您的代码,一切正常,只更新了一行。我只想建议你们一个简单的方法来检查记录是否存在。可能是这样的:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if
我猜这应该被认为是驱动程序中的一个bug;您认为它应该只更新所选记录是正确的(据我所知,我不经常使用ADO或ODBC)。包含主键的解决方案是一个很好的解决方案,或者您可以包含伪列
RDB$DB_key
,它也可以标识记录。@mark Rotterveel,在阅读您的注释后,我使用ADO和Access数据库尝试了相同的代码。只更新了一条记录,支持您的建议,即这是firebird odbc驱动程序中的一个错误。如果是这样的话,我很高兴,因为否则我做错事已经很久了!非常感谢。你可能想在@mark Rotterveel上创建一个bug报告,是的,我已经做了:-)我想你的意思是:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if