0

我已经构建了一个 xml/aspx 页面,该页面通过 sql server 检索产品表中的所有行。此页面用作 xmlfeed。此 xml 显示产品详细信息,如名称、价格和库存等。因为查询非常耗时,我只想检索已更改的行(例如库存)。在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();
    }
4

1 回答 1

0

您需要在数据库中添加另一列,以显示上次更新行的时间。它可以像添加一个日期时间列 ModifidedDate 一样简单。如果你想更进一步,你甚至可以添加 ModifiedBy 之类的东西……</p>

完成此操作后,只需更新您的存储过程以包含基本上说的 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;
}
于 2013-06-27T09:13:26.243 回答