在 VB6 中更新记录集
Updating recordset in VB6
快速提问,这个网站的新手,尤其是编程新手。
我正在尝试 Select 来自 SqlServer 的数据,然后在它仍然是记录集时更新它。我不想用数据更新实际的 table....
因此,如果我有一个名为 RS!("FirstName") 的字段,并且我想向其中添加一个字符串,例如 "MR"
我就是这样做的
str = "Select FirstName from tblClient"
rs.Open SQL, g_cn, adOpenStatic
rs.movefirst
do while not rs.eof
rs("FirstName") = "MR" & rs("FirstName") <--- this is what i'm trying to do but it tells me I cannot update it.
rs.movenext
loop
如何解决这个问题
您应该显示所有代码,以便更容易地帮助您。
根据您所展示的内容 - 一个明显的解决方法是使用您的 str 变量。
如果您在模块的最顶部添加一个 Option Explicit ,它会显示得更快,然后您可以告诉您没有声明一些变量
在任何情况下,如果您使用您声明的 sql - 并且 - 更改您的记录集类型,它应该可以工作
变化:
str = "Select FirstName from tblClient"
rs.Open SQL, g_cn, adOpenStatic
收件人:
str = "Select FirstName from tblClient"
rs.Open str, g_cn, adOpenDynamic, adLockOptimistic
循环:
rs.Edit
rs("FirstName") = "MR" & rs("FirstName")
rs.Update
rs.movenext
真正的答案是创建一个新的断开连接的记录集。您可以随心所欲地对其进行操作,完成后将其设置为空。您可以专门针对要修改的记录集执行此操作,或者编写一个更通用的方法来复制源记录集。
这是一个通用示例。 rsOriginal 是传入的记录集,后面在代码中使用。 rsUpdateable 是构建为原始副本的本地记录集,然后分配给原始记录集变量。它现在完全可以更新,并且无法将更改保存回源 table。
...
'clone a recordset into a new updateable recordset
Dim rsUpdateable As New ADODB.Recordset
Dim fld As ADODB.Field
'errors here are unlikely, but if happens I don't want to alert the user
On Error Resume Next
'build the table schema
For Each fld In rsOriginal.Fields
rsUpdateable.Fields.Append fld.Name, fld.Type, fld.DefinedSize
Next fld
rsUpdateable.Open
'populate the new recordset with the original values
rsUpdateable.AddNew
For Each fld In rsOriginal.Fields
rsUpdateable.Fields(fld.Name).Value = fld.Value
Next fld
Set rs = rsUpdateable 'done
On Error GoTo OriginalErrorHandler 'restore error handler
...
快速提问,这个网站的新手,尤其是编程新手。
我正在尝试 Select 来自 SqlServer 的数据,然后在它仍然是记录集时更新它。我不想用数据更新实际的 table.... 因此,如果我有一个名为 RS!("FirstName") 的字段,并且我想向其中添加一个字符串,例如 "MR"
我就是这样做的
str = "Select FirstName from tblClient"
rs.Open SQL, g_cn, adOpenStatic
rs.movefirst
do while not rs.eof
rs("FirstName") = "MR" & rs("FirstName") <--- this is what i'm trying to do but it tells me I cannot update it.
rs.movenext
loop
如何解决这个问题
您应该显示所有代码,以便更容易地帮助您。
根据您所展示的内容 - 一个明显的解决方法是使用您的 str 变量。 如果您在模块的最顶部添加一个 Option Explicit ,它会显示得更快,然后您可以告诉您没有声明一些变量
在任何情况下,如果您使用您声明的 sql - 并且 - 更改您的记录集类型,它应该可以工作
变化:
str = "Select FirstName from tblClient"
rs.Open SQL, g_cn, adOpenStatic
收件人:
str = "Select FirstName from tblClient"
rs.Open str, g_cn, adOpenDynamic, adLockOptimistic
循环:
rs.Edit
rs("FirstName") = "MR" & rs("FirstName")
rs.Update
rs.movenext
真正的答案是创建一个新的断开连接的记录集。您可以随心所欲地对其进行操作,完成后将其设置为空。您可以专门针对要修改的记录集执行此操作,或者编写一个更通用的方法来复制源记录集。
这是一个通用示例。 rsOriginal 是传入的记录集,后面在代码中使用。 rsUpdateable 是构建为原始副本的本地记录集,然后分配给原始记录集变量。它现在完全可以更新,并且无法将更改保存回源 table。
...
'clone a recordset into a new updateable recordset
Dim rsUpdateable As New ADODB.Recordset
Dim fld As ADODB.Field
'errors here are unlikely, but if happens I don't want to alert the user
On Error Resume Next
'build the table schema
For Each fld In rsOriginal.Fields
rsUpdateable.Fields.Append fld.Name, fld.Type, fld.DefinedSize
Next fld
rsUpdateable.Open
'populate the new recordset with the original values
rsUpdateable.AddNew
For Each fld In rsOriginal.Fields
rsUpdateable.Fields(fld.Name).Value = fld.Value
Next fld
Set rs = rsUpdateable 'done
On Error GoTo OriginalErrorHandler 'restore error handler
...