using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace DAL
{
public class ProductCategoryDAL
{
#region 綁定產品分類下拉框
/// <summary>
/// 綁定產品分類下拉框
/// </summary>
/// <returns></returns>
public void BindProductCategoryList(DropDownList ddl)
{
foreach (DataRow dr in GetCategoryList().Rows)
{
ddl.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
}
}
#endregion
#region 綁定父級產品分類下拉框
/// <summary>
/// 綁定父級產品分類下拉框
/// </summary>
/// <returns></returns>
public void BindUpperProductCategoryList(DropDownList ddl)
{
foreach (DataRow dr in GetUpperCategoryList().Rows)
{
ddl.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
}
}
#endregion
#region 綁定子級產品分類下拉框
/// <summary>
/// 綁定子級產品分類下拉框
/// </summary>
/// <returns></returns>
public void BindUpperProductCategoryList(DropDownList ddl)
{
foreach (DataRow dr in GetChildCategoryList().Rows)
{
ddl.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
}
}
#endregion
/// <summary>
/// 查詢所有產品分類
/// </summary>
/// <returns></returns>
public DataTable GetCategoryList()
{
return DbHelperSQL.Query("select * from tbproductcategory").Tables[0];
}
/// <summary>
/// 查詢父級產品分類
/// </summary>
/// <returns></returns>
public DataTable GetUpperCategoryList()
{
return DbHelperSQL.Query("select * from tbproductcategory where upperid is null").Tables[0];
}
/// <summary>
/// 查詢子級產品分類
/// </summary>
/// <returns></returns>
public DataTable GetChildCategoryList()
{
return DbHelperSQL.Query("select * from tbproductcategory where upperid is not null").Tables[0];
}
/// <summary>
/// 根據id查詢標題
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string GetTitle(int id)
{
string sqlstr = "select name from tbproductcategory where id=@id";
SqlParameter[] param = {
new SqlParameter("@id",SqlDbType.Int)
};
param[0].Value = id;
return DbHelperSQL.GetSingle(sqlstr, param).ToString();
}
/// <summary>
/// 獲得每一個分類並從子表中獲取該類第一條記錄
/// </summary>
/// <param name="top">想要獲取內容的數目</param>
public DataTable GetFirstOfAllWithCateName(int top)//獲得每條分類的第一條內容
{
return DbHelperSQL.Query("select * ,(select name from tbproductcategory where tbproduct.categoryid=tbproductcategory.id) as categoryname from tbproduct where id in(select top " + top + " min(id) from tbproduct group by categoryid order by categoryid)").Tables[0];
}
}
}