2013-06-26 25 views
0

我已經構建了一個xml/aspx頁面,通過sql server檢索producttable中的所有行。此頁面用作xmlfeed。這個XML顯示productdetails,如名稱,價格和股票等,因爲查詢是耗時,我想只檢索已更改的行(例如股票)。有什麼辦法去這個在SQL Server中?如何在長查詢中檢索只更新的產品?

SQL服務器

select name,price,productid 
from product 

C#

public List<Feed> GetFeed() 
{ 
    DataSet dst = new DataSet(); 
    SqlCommand cmd = null; 
    List<Feed> feedlist = new List<Feed>(); 
    using (SqlConnection conn = new SqlConnection(connectionstring)) 
    { 
     conn.Open(); 
     cmd = new SqlCommand("[GetFeed]", conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     using (SqlDataReader rs = cmd.ExecuteReader()) 
     { 
      while (rs.Read()) 
      { 
       feed feed = new Feed(); 
       feed.productID = rs["ProductID"].ToString(); 
       feed.price = rs["price"].ToString(); 
       feedlist.Add(feed); 
      } 
      rs.NextResult(); 
     } 
    } 
    return feedlist; 
} 


    public class Feed 
{ 
    public string Price { get; set; } 
    public string Name { get; set; } 
    public string ProductID { get; set; } 
} 


    protected void Page_Load(object sender, EventArgs e) 
{ 
var feeds = GetFeed(); 
XElement products = 
     new XElement("products", 
      from p in feeds 
      select new XElement("Product", 
        new XElement("id", p.ProductID), 
        new XElement("Price", p.Price), 
        new XElement("Name", p.Name) 

      ) 
     ); 

    Response.ContentType = "text/xml"; 
    Response.Write(products.ToString()); 
    Response.End(); 
    } 
+0

你能告訴我們一些代碼嗎? – logixologist

+0

請顯示您的查詢,我猜你需要一個'WHERE'子句來過濾結果集。 –

+0

@Karl如何在Where子句中選擇更新的行。我是否需要添加修改的列行? – user603007

回答

0

您需要將另一列添加到您的數據庫顯示,當行上次更新。它可以像添加日期時間列ModifidedDate一樣簡單。如果您想進一步藉此一步完成此只需更新您的存儲過程,你甚至可以添加喜歡的東西和ModifiedBy等...

包括WHERE語句基本上說

ALTER PROCEDURE dbo.[GetFeed] 
(
    @LastModification datetime 
) 
AS 
BEGIN 
SELECT * 
FROM Products 
WHERE ModifiedDate > @LastModification 
END 

你也需要更新代碼以包含GetFeed方法和SqlCommand對象的參數

public List<Feed> GetFeed(DateTime lastModification) 
{ 
DataSet dst = new DataSet(); 
SqlCommand cmd = null; 
List<Feed> feedlist = new List<Feed>(); 
using (SqlConnection conn = new SqlConnection(connectionstring)) 
{ 
    conn.Open(); 
    cmd = new SqlCommand("[GetFeed]", conn); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add(new SqlParameter("@LastModification", lastModification)); 
    using (SqlDataReader rs = cmd.ExecuteReader()) 
    { 
     while (rs.Read()) 
     { 
      feed feed = new Feed(); 
      feed.productID = rs["ProductID"].ToString(); 
      feed.price = rs["price"].ToString(); 
      feedlist.Add(feed); 
     } 
     rs.NextResult(); 
    } 
} 
return feedlist; 
} 
相關問題