Visual Basic 6 ADO更新sql字符串

时间:2013-05-07 15:01:01

标签: sql vb6 adodb recordset

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

' 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

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

1 个答案:

答案 0 :(得分: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只是一个存储我所有数据库设置的对象,只需用我自己的设置替换我的设置。