// use ordinals to speed up access to DataReader // links: // docLink: http://sql2x.org/documentationLink/327451c3-64a8-4de8-b359-76742d634497 // parameters: // reader: IDataReader from SQLClient public void Populate(IDataReader reader, CategoryTreeDataOrdinals ordinals) { if (!reader.IsDBNull(ordinals.ProductCategoryId)) { ProductCategoryId = reader.GetGuid(ordinals.ProductCategoryId); } if (!reader.IsDBNull(ordinals.ProductCategoryParentId)) { ProductCategoryParentId = reader.GetGuid(ordinals.ProductCategoryParentId); } if (!reader.IsDBNull(ordinals.ProductCategoryCode)) { ProductCategoryCode = reader.GetString(ordinals.ProductCategoryCode); } if (!reader.IsDBNull(ordinals.ProductCategoryName)) { ProductCategoryName = reader.GetString(ordinals.ProductCategoryName); } }
/// <summary>Get categories</summary> /// <cardinality>Many</cardinality> public List <CategoryTreeData> CategoryTree() { var ret = new List <CategoryTreeData>(); string sql = @" select pc.product_category_id, pc.product_category_parent_id, pc.product_category_code, pc.product_category_name from product_category as pc where pc.product_category_became_id is null and right(pc.product_category_name, 1) != '-' order by pc.product_category_code ,pc.product_category_position "; using (var conn = new SqlConnection(ConfigurationManager.AppSettings["Conn"])) { conn.Open(); using (var command = new SqlCommand(sql, conn)) { IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); var ordinals = new CategoryTreeDataOrdinals(reader); while (reader.Read()) { var data = new CategoryTreeData(); data.Populate(reader, ordinals); ret.Add(data); } reader.Close(); } return(ret); } }