/// <summary> /// 返回不完整字段的图书 /// </summary> /// <param name="safeSql"></param> /// <returns></returns> private static IList <Book> GetPartialBooksBySql(string safeSql) { List <Book> list = new List <Book>(); DataTable table = DBHelper.GetDataSet(safeSql); foreach (DataRow row in table.Rows) { Book book = new Book(); book.Id = (int)row["Id"]; book.Title = (string)row["Title"]; book.Author = (string)row["Author"]; if (table.Columns.Contains("UnitPrice")) { book.UnitPrice = (decimal)row["UnitPrice"]; } if (table.Columns.Contains("ShortContent")) { book.ContentDescription = (string)row["ShortContent"]; } if (table.Columns.Contains("ISBN")) { book.ISBN = (string)row["ISBN"]; } if (table.Columns.Contains("Clicks")) { book.Clicks = (int)row["Clicks"]; } if (table.Columns.Contains("PublishDate")) { book.PublishDate = (DateTime)row["PublishDate"]; } if (table.Columns.Contains("CategoryId")) { book.Category = CategoryService.GetCategoryById((int)row["CategoryId"]); } book.Publisher = PublisherService.GetPublisherById((int)row["PublisherId"]); //FK list.Add(book); } return(list); }
private static IList <Book> GetBooksBySql(string sql, params SqlParameter[] values) { List <Book> list = new List <Book>(); try { DataTable table = DBHelper.GetDataSet(sql, values); foreach (DataRow row in table.Rows) { Book book = new Book(); book.Id = (int)row["Id"]; book.Title = (string)row["Title"]; book.Author = (string)row["Author"]; book.PublishDate = (DateTime)row["PublishDate"]; book.ISBN = (string)row["ISBN"]; book.WordsCount = (int)row["WordsCount"]; book.UnitPrice = (decimal)row["UnitPrice"]; book.ContentDescription = (string)row["ContentDescription"]; book.AurhorDescription = (string)row["AurhorDescription"]; book.EditorComment = (string)row["EditorComment"]; book.TOC = (string)row["TOC"]; book.Clicks = (int)row["Clicks"]; book.Votes = (int)row["Votes"]; book.TotalRating = (int)row["TotalRating"]; book.Publisher = PublisherService.GetPublisherById((int)row["PublisherId"]); //FK book.Category = CategoryService.GetCategoryById((int)row["CategoryId"]); //FK list.Add(book); } return(list); } catch (Exception e) { Console.WriteLine(e.Message); throw e; } }