Asp.net源码专业站
首页->企业网站->中小企业网站系统前台源码(SmallBusinessStarterKit)>>App-Code/Catalog/SqlCatalogProvider.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:中小企业网站系统前台源码(SmallBusinessStarterKit)
当前文件:文件类型 SmallBusinessStarterKit/App_Code/Catalog/SqlCatalogProvider.cs[6K,2009-6-12 11:54:21]打开代码结构图
普通视图
		            
1using System; 2using System.Configuration; 3using System.Web.Configuration; 4using System.Data; 5using System.Data.SqlClient; 6using System.Collections.Generic; 7 /// <summary> 8 ///提供访问SQL Server数据库的方法 9 /// </summary> 10public class SqlCatalogProvider : CatalogProvider 11{ 12 //从Web.Config配置文件中获取连接字符串信息。 13 private string connectionString() 14 { 15 SmallBusinessDataProvidersSection sec = (ConfigurationManager.GetSection("SmallBusinessDataProviders")) as SmallBusinessDataProvidersSection; 16 string connectionStringName = sec.CatalogProviders[sec.CatalogProviderName].Parameters["connectionStringName"]; 17 return WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; 18 } 19 ///<summary> 20 /// 返回属于指定分类ID的产品列表。如果产品中没有价格,则Double.MinValue被返回。 21 /// </summary> 22 public override List<Item> GetChildItems(string parentCategoryId) 23 { 24 List<Item> list = new List<Item>(); 25 //参数检验 26 if (String.IsNullOrEmpty(parentCategoryId)) return list; 27 // 连接到数据源 28 using (SqlConnection con = new SqlConnection(connectionString())) 29 { 30 con.Open(); 31 //使用存储过程GetChildItems获取产品列表 32 SqlCommand cmd = new SqlCommand("GetChildItems", con); 33 cmd.CommandType = CommandType.StoredProcedure; 34 //初始化参数 35 cmd.Parameters.Add("@categoryId", SqlDbType.NVarChar); 36 cmd.Parameters["@categoryId"].Value = parentCategoryId; 37 //执行存储过程返回SqldataReader. 38 SqlDataReader r = cmd.ExecuteReader(); 39 Item curr; 40 //遍历SqlDataRader对象,用数据初始化Item实体类,并添加到List<Item>泛型集合。 41 while (r.Read()) 42 { 43 if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull) 44 throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing); 45 46 curr = new Item((string) r["id"], 47 (Boolean)r["visible"], 48 (string) r["title"]); 49 curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"]; 50 curr.Price = (r["price"] is DBNull) ? Double.MinValue : (double)r["price"]; 51 curr.InStock = (r["inStock"] is DBNull) ? true : (Boolean)r["inStock"]; 52 curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 53 curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 54 list.Add(curr); 55 } 56 } 57 //返回列表 58 return list; 59 } 60 ///<summary> 61 ///返回指定分类下面的子分类。 62 ///</summary> 63 public override List<Category> GetChildCategories(string parentCategoryId) 64 { 65 List<Category> list = new List<Category>(); 66 using (SqlConnection con = new SqlConnection(connectionString())) 67 { 68 con.Open(); 69 SqlCommand cmd; 70 if( String.IsNullOrEmpty(parentCategoryId)) 71 { 72 //使用GetRootCategories方法获取子分类数据。 73 cmd = new SqlCommand("GetRootCategories", con); 74 } 75 else 76 { 77 //如果没有指定分类ID,则使用GetNonRootCategories存储过程获取分类数据。 78 cmd = new SqlCommand("GetNonRootCategories ", con); 79 cmd.CommandType = CommandType.StoredProcedure; 80 cmd.Parameters.Add("@categoryId", SqlDbType.NVarChar); 81 cmd.Parameters["@categoryId"].Value = parentCategoryId; 82 } 83 //返回SqldataReader 84 SqlDataReader r = cmd.ExecuteReader(); 85 Category curr; 86 //遍历SqlDataReader对象,将其中的数据赋给Category实体对象,并将Category实体对象添加到List<Catagory>中。 87 while (r.Read()) 88 { 89 if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull) 90 throw new InvalidOperationException(Messages.CategoryRequiredAttributesMissing); 91 92 curr = new Category((string)r["id"],(Boolean)r["visible"],(string)r["title"]); 93 curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"]; 94 curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 95 curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 96 list.Add(curr); 97 } 98 } 99 return list; 100 } 101 ///<summary> 102 ///根据指定的ItemID返回产品 103 ///</summary> 104 public override Item GetItem(string itemId) 105 { 106 //检查参数 107 if (String.IsNullOrEmpty(itemId)) return null; 108 // 连接到数据库 109 Item curr; 110 using (SqlConnection con = new SqlConnection(connectionString())) 111 { 112 con.Open(); 113 SqlCommand cmd = new SqlCommand("GetItem", con); 114 cmd.CommandType = CommandType.StoredProcedure; 115 cmd.Parameters.Add("@itemId", SqlDbType.NVarChar); 116 cmd.Parameters["@itemId"].Value = itemId; 117 //调用GetItem存储过程,返回SqlDataReader对象。 118 SqlDataReader r = cmd.ExecuteReader(); 119 r.Read(); 120 //遍历SqlDataReader对象,返回一个被赋值的Item实体对象。 121 if (r["id"] is DBNull || r["visible"] is DBNull || r["title"] is DBNull) 122 throw new InvalidOperationException(Messages.ItemRequiredAttributesMissing); 123 124 curr = new Item((string)r["id"], 125 (Boolean)r["visible"], 126 (string)r["title"]); 127 curr.Description = (r["description"] is DBNull) ? String.Empty : (string)r["description"]; 128 curr.Price = (r["price"] is DBNull) ? Double.MinValue : (double)r["price"]; 129 curr.InStock = (r["inStock"] is DBNull) ? true : (Boolean)r["inStock"]; 130 curr.ImageUrl = (r["imageUrl"] is DBNull) ? String.Empty : (string)r["imageUrl"]; 131 curr.ImageAltText = (r["imageAltText"] is DBNull) ? String.Empty : (string)r["imageAltText"]; 132 } 133 return curr; 134 } 135} 136
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:中小企业网站系统前台源码(SmallBusinessStarterKit)
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146