我要从 SQL 获得一个产品图像列表, 以在图像幻灯片中显示 。
然而,我不知道退还MS SQL存储程序数据的最佳方式。
问题是,在下面的 SP 中, 每张图像都有一个重复的记录。 理想的情况是, 我想从一个记录中找到一个阵列或字符串列表或某种方法来分割数据。 但是, 我愿意接受其他解决方案。 我的目标是以直观的方式将图像放入页面。 这是用于 ASP. Net C# 应用程序 。
这是我存储程序的简化选择语句:
SELECT
P.[ProductId]
,P.[ProductName]
,I.[FileName] as ProductImage
FROM [Product] P
LEFT JOIN [ProductImages] I
on P.ProductId = I.ProductId
Where P.ProductId = @ProductId
此返回类似此值的数据 :
ProductId ProductName ProductImage
1 Coffee Mug Mug_Image1.jpg
1 Coffee Mug Mug_2.jpg
1 Coffee Mug Mug_Img3.jpg
我喜欢这样看(但我也想听听其他想法):
ProductId ProductName ProductImage
1 Coffee Mug Mug_Image1.jpg, Mug_2.jpg, Mug_Img3.jpg
再说一遍,我不知道这是不是最好的办法。
我有两张桌子,一张是产品图,另一张是产品图,它从产品表上有一个FK到产品图。
最后,我需要建造JSON 来喂饱客户的幻灯片脚本。
productimages = [
{ "image": "Mug_Image1.jpg", "caption": "", "link": "", "title": "" },
{ "image": "Mug_2.jpg", "caption": "", "link": "", "title": "" },
{ "image": "Mug_Img3.jpg", "caption": "", "link": "", "title": "" }
];
这里是C
//Create product object to display on page
ProductEntity product = new ProductEntity();
product.Id = idval;
//Load the product from SP based on ID
ProductWF.LoadProduct(product);
//Render product details on page
ProductNameLabel.Text = product.ProductName;
//Construct JSON image list
StringBuilder sb = new StringBuilder();
etc...