使用VB.NET更新访问记录集

时间:2012-01-11 01:11:40

标签: database vb.net ms-access

如何使用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

2 个答案:

答案 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