温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:我的小书坊源码(三层实现)
当前文件:
MyBookShop/App_Code/BusinessLogicLayer/Book.cs,打开代码结构图
MyBookShop/App_Code/BusinessLogicLayer/Book.cs,打开代码结构图1using System; 2
using System.Collections; 3
using System.Data; 4
5
using MyBookShop.DataAccessLayer; 6
using MyBookShop.DataAccessHelper; 7
8
9
namespace MyBookShop.BusinessLogicLayer 10
...{ 11
/**//// <summary> 12
/// 图书对象 13
/// </summary> 14
public class Book 15
...{ 16
私有成员#region 私有成员 17
18
private int _bookId; //图书ID 19
private string _bookName; //图书名 20
private int _categoryId; //分类ID 21
private double _price; //价格 22
private string _publisher; //出版社 23
private DateTime _publishDate; //出版时间 24
private string _author; //作者 25
private int _pageNum; //页数 26
private string _pictureUrl; //图片路径 27
private string _description; //描述 28
private int _saleCount; //销售量 29
30
private bool _exist; //是否存在标志 31
32
#endregion 私有成员 33
34
属性#region 属性 35
36
public int BookID 37
...{ 38
set 39
...{ 40
this._bookId=value; 41
} 42
get 43
...{ 44
return this._bookId; 45
} 46
} 47
public string BookName 48
...{ 49
set 50
...{ 51
this._bookName=value; 52
} 53
get 54
...{ 55
return this._bookName; 56
} 57
} 58
public int CategoryID 59
...{ 60
set 61
...{ 62
this._categoryId=value; 63
} 64
get 65
...{ 66
return this._categoryId; 67
} 68
} 69
public double Price 70
...{ 71
set 72
...{ 73
this._price=value; 74
} 75
get 76
...{ 77
return this._price; 78
} 79
} 80
public string Publisher 81
...{ 82
set 83
...{ 84
this._publisher=value; 85
} 86
get 87
...{ 88
return this._publisher; 89
} 90
} 91
public DateTime PublishDate 92
...{ 93
set 94
...{ 95
this._publishDate=value; 96
} 97
get 98
...{ 99
return this._publishDate; 100
} 101
} 102
public string Author 103
...{ 104
set 105
...{ 106
this._author=value; 107
} 108
get 109
...{ 110
return this._author; 111
} 112
} 113
public int PageNum 114
...{ 115
set 116
...{ 117
this._pageNum=value; 118
} 119
get 120
...{ 121
return this._pageNum; 122
} 123
} 124
public string PictureUrl 125
...{ 126
set 127
...{ 128
this._pictureUrl=value; 129
} 130
get 131
...{ 132
return this._pictureUrl; 133
} 134
} 135
public string Description 136
...{ 137
set 138
...{ 139
this._description=value; 140
} 141
get 142
...{ 143
return this._description; 144
} 145
} 146
public int SaleCount 147
...{ 148
set 149
...{ 150
this._saleCount=value; 151
} 152
get 153
...{ 154
return this._saleCount; 155
} 156
} 157
public bool Exist 158
...{ 159
get 160
...{ 161
return this._exist; 162
} 163
} 164
165
#endregion 属性 166
167
方法#region 方法 168
169
/**//// <summary> 170
/// 根据参数bookID,获取图书详细信息 171
/// </summary> 172
/// <param name="topicID">图书ID</param> 173
public void LoadData(int bookID) 174
...{ 175
Database db=new Database(); //实例化一个Database类 176
177
string sql=""; 178
sql="Select * from [Book] where BookID = "+ bookID ; 179
180
DataRow dr=db.GetDataRow(sql); //利用Database类的GetDataRow方法查询用户数据 181
182
//根据查询得到的数据,对成员赋值 183
if(dr!=null) 184
...{ 185
this._bookId=GetSafeData.ValidateDataRow_N(dr,"BookID"); 186
this._bookName=GetSafeData.ValidateDataRow_S(dr,"BookName"); 187
this._categoryId=GetSafeData.ValidateDataRow_N(dr,"CategoryId"); 188
this._price=GetSafeData.ValidateDataRow_F(dr,"Price"); 189
this._publisher=GetSafeData.ValidateDataRow_S(dr,"Publisher"); 190
this._publishDate=GetSafeData.ValidateDataRow_T(dr,"PublishDate"); 191
this._author=GetSafeData.ValidateDataRow_S(dr,"Author"); 192
this._pageNum=GetSafeData.ValidateDataRow_N(dr,"PageNum"); 193
this._pictureUrl=GetSafeData.ValidateDataRow_S(dr,"PictureUrl"); 194
this._description=GetSafeData.ValidateDataRow_S(dr,"Description"); 195
this._saleCount=GetSafeData.ValidateDataRow_N(dr,"SaleCount"); 196
197
this._exist=true; 198
} 199
else 200
...{ 201
this._exist=false; 202
} 203
} 204
205
/**//// <summary> 206
/// 向数据库添加一本图书 207
/// </summary> 208
/// <param name="topicInfo">图书信息哈希表</param> 209
public void Add(Hashtable bookInfo) 210
...{ 211
Database db=new Database(); //实例化一个Database类 212
db.Insert("[Book]",bookInfo ); //利用Database类的Inser方法,插入数据 213
} 214
215
/**//// <summary> 216
/// 修改图书内容 217
/// </summary> 218
/// <param name="newBookInfo">新的图书信息哈希表</param> 219
/// <param name="condition">Update的Where子句</param> 220
public void Update(Hashtable newBookInfo) 221
...{ 222
Database db=new Database(); 223
string condition = "Where BookID = "+this._bookId; 224
db.Update("[Book]",newBookInfo,condition); 225
} 226
227
/**//// <summary> 228
/// 删除图书 229
/// </summary> 230
public void Delete() 231
...{ 232
Database db=new Database(); 233
string strSql="Delete From [Book] Where BookID = "+this._bookId; 234
db.ExecuteSQL(strSql); 235
} 236
237
/**//// <summary> 238
/// 查询图书信息 239
/// </summary> 240
/// <param name="queryItems">查询条件哈希表</param> 241
/// <returns>查询结果集</returns> 242
public static DataTable QueryBooks(Hashtable queryItems) 243
...{ 244
string where=SqlStringConstructor.GetConditionClause(queryItems); 245
string sql="Select * From [Book] "+where; 246
247
Database db = new Database(); 248
return db.GetDataTable(sql); 249
} 250
251
/**//// <summary> 252
/// 按某个属性,升序或者降序获取所有的图书信息 253
/// </summary> 254
/// <param name="sortedColumn">排序属性</param> 255
/// <param name="sortType">可取{desc,asc},分别代表降序、升序</param> 256
/// <returns>查询结果集</returns> 257
public static DataTable QueryBooks(Hashtable queryItems,string sortedColumn,string sortType) 258
...{ 259
if(sortType.ToUpper()!="DESC" && sortType.ToUpper()!="ASC" && sortType!="") 260
return null; 261
262
string where=SqlStringConstructor.GetConditionClause(queryItems); 263
string constrains=" Order By "+sortedColumn+" "+sortType; 264
string sql="Select * From [Book] "+where+constrains; 265
266
Database db = new Database(); 267
return db.GetDataTable(sql); 268
}




