如何使用VB.NET更新MS-Access记录集?
我基本上要更新id为1的字段,'ticker'的字段值为'UKX'。
要更新的数据位于“值”字段中,来自Public Sub。 dLast是我需要输入以更新当前值的数据。
我目前有代码:
Function update_db()
' need to update the database
Dim daoengine As DAO.DBEngine
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
daoengine = New DAO.DBEngine
dbs = daoengine.OpenDatabase("Project.mdb")
rst = dbs.OpenRecordset("ftsedata", DAO.RecordsetTypeEnum.dbOpenDynaset)
rst.Edit()
' what do I do here to edit the field "value" where ticker=UKX ?
rst.Close()
dbs.Close()
End Function
Public Sub getftsestock()
Dim sAPIUrl As String = "http://www.google.com/ig/api?stock=UKX"
Dim oDocument As XDocument = XDocument.Load(sAPIUrl)
Dim dLast As Double = CDbl(GetData(oDocument, "last"))
End Sub
答案 0 :(得分:1)
首先,将您的sub更改为类似的函数
Public function getftsestock() as double
Dim sAPIUrl As String = "http://www.google.com/ig/api?stock=UKX"
Dim oDocument As XDocument = XDocument.Load(sAPIUrl)
getftsestock = CDbl(GetData(oDocument, "last"))
End Sub
然后像这样进行更新
rst = dbs.OpenRecordset("select * from ftsedata where (ticker='UKX')", DAO.RecordsetTypeEnum.dbOpenDynaset)
while not rsy.eof
rst.Edit
rst("value").value = getftsestock()
rst.update
rst.movenext
wend
rst.Close()
然而,这样做会更有效率。
daoengine.Execute "Update ftsedata SET value='" & getftsestock() & "' WHERE ticker='UKX'"
答案 1 :(得分:0)
您必须调用Database对象的Execute方法。
daoengine.Execute "Update TableName set Col1=Value1 Where Ticker='UKX'"
您必须在VB.NET中使用ADO.NET Oledb
提供程序而不是DAO
。