English 中文(简体)
SQLSP 产品图像返回列表
原标题:SQL SP Return List of Product Images

我要从 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...
最佳回答

你可以用这样的东西:

SELECT
   P.[ProductId],
   P.[ProductName],
   FileNames = STUFF(
               (SELECT  ,   + FileName FROM ProductImages 
                WHERE ProductId = P.ProductId FOR XML PATH(  )) , 1 , 1 ,   )
      FROM 
        [Product] P

来获取每个产品的逗号分隔文件名列表, 如果有用的话。 但是您想要生成的 JSON 字符串可能比您已有的查询结果简单得多 。

如果您从上面取出查询, 您可以将其映射为类 :

public class Product
{
   public int ProductId {get; set;}
   public string ProductName {get; set;}
   public string FileNames {get; set;}
}

在服务器侧面,您需要拆分文件名称字符串属性并构建json字符串。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签