ASP.NET:循环数据库

时间:2012-06-12 05:50:58

标签: asp.net database loops

我现在有一个100%的功能页面,它根据通过querystring发送的id更新数据库。现在,我想让这个函数执行数据库中的每一行,而不是发送查询字符串。

 Dim amzRepeater As DataRowView = e.Item.DataItem


    Dim myId As String = Request.QueryString("Id")
    Dim amzUrl As String = amzRepeater.Row("AmazonUrl")
    amazonURL.Value = amzUrl


    '''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Dim aHtml As New HtmlWeb
    Dim aDoc As HtmlDocument = aHtml.Load(amazonURL.Value)


    Dim aNode As HtmlAgilityPack.HtmlNode
    aNode = aDoc.DocumentNode.SelectSingleNode("//div[@id='olpDivId']/span[2]")
    Dim UsedPrice As String = aNode.InnerText
    Dim i As Integer = UsedPrice.IndexOf("$")
    UsedPrice = UsedPrice.Substring(i + 1)

    Dim connStr As String = ConfigurationManager.ConnectionStrings("arprimaryConnectionString").ConnectionString
    Dim con As New SqlConnection(connStr)
    Dim cmd As New SqlCommand("UPDATE Amazon SET AmazonUsedPrice = @AmazonUsedPrice, LastUpdated = @LastUpdated WHERE (id = @id)", con)
    cmd.Parameters.AddWithValue("@AmazonUsedPrice", UsedPrice)
    cmd.Parameters.AddWithValue("@LastUpdated", DateTime.Now.ToString)
    cmd.Parameters.AddWithValue("@Id", myId)

    Using con
        con.Open()
        cmd.ExecuteNonQuery()
    End Using

    MsgBox("Update Sucesfull.")

1 个答案:

答案 0 :(得分:0)

您应该只能从SQL语句和 id 参数中删除 WHERE 子句。这将更新每一行:

Dim cmd As New SqlCommand("UPDATE Amazon SET AmazonUsedPrice = @AmazonUsedPrice, LastUpdated = @LastUpdated", con)
cmd.Parameters.AddWithValue("@AmazonUsedPrice", UsedPrice)
cmd.Parameters.AddWithValue("@LastUpdated", DateTime.Now.ToString)