1

我正在尝试使用记录集更新 sql 中的单个记录我正在从一个记录集中提取数据并尝试将其保存到数据库中的另一个表这是我的插入语句在 SQL 中运行良好的代码。

' Opening the connection
cn.ConnectionString = "Provider=SQLOLEDB; Data Source=" & dbLocation & "; Initial Catalog=Posbdat; User Id=sa"
cn.Open
rh.ConnectionString = "Provider=SQLOLEDB; Data Source=" & dbLocation & "; Initial Catalog=Postrans; User Id=sa"
rh.Open

 rs.Open "Select Top 1 * from CustomerPoints order by RedemptionDate desc", cn, adModeReadWrite
x.Open " Select Top 1 * from Register_Header order by datetime desc", rh, adModeReadWrite

rt.Open " SELECT top 1 upc FROM   Register_Trans INNER JOIN Register_Header ON Register_Trans.trans_no = Register_Header.trans_no Where trans_subtype  = 'AP' Order by Register_Trans.datetime desc ", rh, adOpenDynamic

Debug.Print x!emp_no
 Debug.Print x!till_no
 Debug.Print x.Fields(10)
 Debug.Print itemupc
itemupc = rt.Fields(0)
Dim cmd As New ADODB.Recordset
 cmd.Open "UPDATE CustomerPoints set emp_no = " & x!emp_no & _
 ", till_no = " & x!till_no & " purch_amt = " & x!Total & _
 ", item_redeem = ' " & itemupc & " ' Where RedemptionDate = (Select top 1 * from CustomerPoints order by " & _
 "RedemptionDate Desc)", cn, adOpenDynamic
cmd.update

我没有关闭任何连接或清理它,因为它不会在不崩溃的情况下运行。

4

1 回答 1

2

已经很长时间了,但这就是我用来执行更新语句的方式:

Dim conTemp As New ADODB.Connection

conTemp.CommandTimeout = mvarconConnection.CommandTimeout
conTemp.ConnectionTimeout = mvarconConnection.ConnectionTimeout
conTemp.CursorLocation = mvarconConnection.CursorLocation
conTemp.Mode = mvarconConnection.Mode
conTemp.ConnectionString = mvarconConnection.ConnectionString
conTemp.Open mvarconConnection.ConnectionString

conTemp.Execute "SET CONCAT_NULL_YIELDS_NULL OFF"

conTemp.Execute "UPDATE CustomerPoints set emp_no = " & x!emp_no & _
", till_no = " & x!till_no & " purch_amt = " & x!Total & _
", item_redeem = ' " & itemupc & " ' Where RedemptionDate = (Select top 1 * from
CustomerPoints order by " & _
"RedemptionDate Desc)"

mvarconConnection 只是一个存储我所有数据库设置的对象,只需将我的设置替换为您自己的设置即可。

于 2013-05-07T15:24:38.943 回答