2012-06-12 87 views
0

我現在有一個頁面,功能100%,它更新數據庫,基於通過查詢字符串發送的ID。現在,我想讓這個函數對數據庫中的每一行執行,而不需要發送查詢字符串。ASP.NET:通過數據庫循環

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.") 
+0

您希望每行都具有相同的@AmazonUsedPrice? – rikitikitik

回答

0

你應該能夠只從SQL語句刪除WHERE子句ID參數。這將更新每一行:

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)