如何在具有相同名称的情况下分隔不同的数据库值?

时间:2010-09-03 09:53:34

标签: sql data-binding

我在从数据库中获取正确的“名称”时遇到问题,结果在所有“名称”上都是相同的。我从同一存储过程中获取信息。有没有办法指定我寻找的女巫名字? 恩。 Text='<%#Eval("tblBrand.Name") %>'获取tblBrand中的名称。但那种剂量有效。

<asp:Label ID="lblProductName" runat="server" Text='<%#Eval("Name") %>' CssClass="productHead" />

<asp:Label ID="lblModelName" runat="server" Text='<%#Eval("Name") %>' CssClass="productHead" />

<asp:Label ID="lblSubCategoryName" runat="server" Text='<%#Eval("Name") %>' CssClass="productHead" />

<asp:Label ID="lblBrandName" runat="server" Text='<%#Eval("Name") %>' CssClass="productHead" />



SELECT
    Product.ProductID, Product.Name, tblBrand.Name, SubCategory.Name, 
     tblModel.Name
FROM            Product INNER JOIN
                         tblBrand ON Product.BrandID = tblBrand.BrandID INNER JOIN
                         tblModel ON Product.ModelID = tblModel.ModelID INNER JOIN
                         SubCategory ON Product.SubCategoryID = SubCategory.SubCategoryID
WHERE        (Product.ProductID = @ProductID)

2 个答案:

答案 0 :(得分:2)

对列进行别名,以便数据明确地收缩。

SELECT
    Product.ProductID,
    Product.Name AS ProductName,
    tblBrand.Name AS BrandName,
    SubCategory.Name AS SubCategoryName, 
    tblModel.Name AS ModelName 
....

答案 1 :(得分:1)

通常我会对查询中的字段进行别名,然后引用别名:

<asp:Label ID="lblProductName" runat="server" Text='<%#Eval("ProductName") %>' CssClass="productHead" />

<asp:Label ID="lblModelName" runat="server" Text='<%#Eval("ModelName") %>' CssClass="productHead" />

<asp:Label ID="lblSubCategoryName" runat="server" Text='<%#Eval("SubCategoryName") %>' CssClass="productHead" />

<asp:Label ID="lblBrandName" runat="server" Text='<%#Eval("BrandName") %>' CssClass="productHead" />



SELECT        Product.ProductID, Product.Name ProductName, tblBrand.Name BrandName, SubCategory.Name SubCategoryName, tblModel.Name ModelName
FROM Product INNER JOIN tblBrand ON Product.BrandID = tblBrand.BrandID INNER JOIN tblModel ON Product.ModelID = tblModel.ModelID INNER JOIN SubCategory ON Product.SubCategoryID = SubCategory.SubCategoryID WHERE (Product.ProductID = @ProductID)