在ASP.NET中获取类别和子类别名称

时间:2016-02-06 11:14:48

标签: c# sql asp.net sql-server

我有一个TBLCATEGORIES和一个TBLPRODUCTS表。我想使用连接在 Products.aspx 页面上获取类别和子类别名称:

Table

以下代码无效:

DataTable dtProducts = system.GetDataTable(@"Select b.CategoryName,  
        a.* from TBLPRODUCTS a LEFT JOIN TBLCATEGORIES b ON 
        (A.SubCategoryID=B.CategoryID) order by a.ProductID desc");
if (dtProducts.Rows.Count > 0)
{
    CollectionPager1.DataSource = dtProducts.DefaultView;
    CollectionPager1.BindToControl = rpProducts;

    rpProducts.DataSource = CollectionPager1.DataSourcePaged;
    rpProducts.DataBind();
}

1 个答案:

答案 0 :(得分:0)

我猜你需要两个连接:

Select p.*, c.CategoryName, sc.CategoryName as SubCategoryName
from TBLPRODUCTS p left join
     TBLCATEGORIES c
     on p.CategoryId = c.CategoryId left join
     TBLCATEGORIES sc
     on c.SubCategoryId = sc.CateogryId
order by p.ProductID desc;