public ActionResult Create(Book bk, HttpPostedFileBase coverimg) { string filePath = ""; if (coverimg.ContentLength > 0) { TempData["Message"] = Helper.MsgBox.GetMsg("success", "IF ", "Your data has been added"); string filename = Guid.NewGuid().ToString() + "_" + coverimg.FileName; filePath = Path.Combine(HttpContext.Server.MapPath("~/Content/Images"), filename); bk.CoverImage = filename; coverimg.SaveAs(filePath); } using (BookDAL svBooks = new BookDAL()) { try { svBooks.Add(bk); TempData["Message"] = Helper.MsgBox.GetMsg("success", "Success! ", "Your data has been added"); } catch (Exception ex) { TempData["Message"] = Helper.MsgBox.GetMsg("danger", "Error !", ex.Message); } } return(RedirectToAction("Index")); }
private void GenerateBooks() { var random = new Random(); List <Author> authors = (List <Author>)Session["Authors"]; Session["Books"] = new List <Book>(); int nbAuthors = authors.Count; DateTime startDate = new DateTime(1800, 01, 01); DateTime endDate = new DateTime(2017, 06, 09); var dal = new BookDAL((List <Book>)Session["Books"]); for (int i = 0; i < random.Next(10, 20); i++) { String bookTitle = random.NextLoremIpsum(2); DateTime date = random.NextDate(startDate, endDate); String edition = random.NextString(8); int quantity = random.Next(10); String thematic = random.NextString(8); var selectedIndexAuthor = random.Next(0, nbAuthors - 1); Author selectedAuthor = authors.ElementAt(selectedIndexAuthor); Book book = new Book() { Author = selectedAuthor, AvailableQuantity = quantity, Edition = edition, PublicationDate = date, Thematic = thematic, Title = bookTitle }; dal.Add(book); } }
public void CreateNewBookAndAddToDB() { MockAuthors(); Book book = new Book() { Title = "title", ReleaseDate = DateTime.Now, Description = "desc", Note = "note", Authors = _author_dal.GetAll().Select(x => new Author(x)).ToList() }; _book_dal.Add(book); _book_dal.Save(); Assert.AreEqual(1, _book_dal.GetAll().Count); }
[ValidateAntiForgeryToken] // Création d'un livre à partir d'infos récupéré via un form public ActionResult Create([Bind(Include = "book, authorSelected")] AddBookViewModel model) { // Récupération des datas BookDAL dal = new BookDAL((List <Book>)Session["Books"]); AuthorDAL authorDAL = new AuthorDAL((List <Author>)Session["Authors"]); List <Author> authors = authorDAL.GetAll(); model.authors = GetSelectListItems(authors); // Récupération de l'auteur Author selectedAuthor = authorDAL.Read(model.authorSelected); model.book.Author = selectedAuthor; // Ajout de l'auteur au model if (ModelState.IsValid) { dal.Add(model.book); // Ajout du livre return(RedirectToAction("Index")); // Si tout se passe bien, afficher liste livre } return(View("Add", model)); // Si erreur, laissez la view d'ajout }
private void AddBook(object sender, EventArgs e) { //语言,0-中文,1-英文,2-日文,3-俄文,4-德文,5-法文 short lang = 0; switch (comboBox1.Text) { case "中文": lang = 0; break; case "英文": lang = 1; break; case "日文": lang = 2; break; case "俄文": lang = 3; break; case "德文": lang = 4; break; case "法文": lang = 5; break; } byte[] picbyte; if (this.picturePath != null) { picbyte = Tools.GetBytesByImagePath(this.picturePath); } else { picbyte = Tools.GetBytesByImage(pictureBox1); }; if ("" == textBox9.Text) { textBox9.Text = "0"; } Book book = new Book(); book.bkAuthor = textBox2.Text; book.bkBrief = textBox6.Text; book.bkCatalog = textBox7.Text; book.bkCode = textBox8.Text; book.bkCover = picbyte; book.bkDateIn = DateTime.Now; label10.Text = book.bkDateIn.ToString(); book.bkDatePress = dateTimePicker1.Value; book.bkISBN = textBox5.Text; book.bkLanguage = lang; if ("" == textBox1.Text) { MessageBox.Show("书名不能为空!"); return; } else { book.bkName = textBox1.Text; } try{ book.bkPages = int.Parse(textBox9.Text); }catch (Exception pgerr) { MessageBox.Show("页码错误!\n" + pgerr.Message); return; } book.bkPress = textBox3.Text; try { book.bkPrice = float.Parse(textBox4.Text); }catch (Exception prieer) { MessageBox.Show("价格错误!\n" + prieer.Message); return; } book.bkStatus = "在馆"; try { if (BookDAL.Add(book) > 0) { MessageBox.Show("添加成功!"); } }catch (Exception ex) { MessageBox.Show(ex.Message + "\n添加失败!"); } }
public static void ConnectDataBase() { #region DAL.ReaderType测试 Console.WriteLine("DAL.SqlHelper:测试数据库连接..."); SqlHelper.OpenConn(); Console.WriteLine("DAL.SqlHelper:测试数据库关闭...\n"); SqlHelper.CloseConn(); Console.WriteLine("Model.ReaderType:测试创建数据单元..."); ReaderType rdt1 = new ReaderType(); rdt1.rdType = 1; rdt1.rdTypeName = "admin"; rdt1.maxBorrowDay = 30; rdt1.maxBorrowNum = 8; rdt1.maxContinueTimes = 1; Console.WriteLine("DAL.ReaderType:测试Delete()..."); ReaderTypeDAL.Delete(rdt1); Console.WriteLine("DAL.ReaderType:测试Add()..."); rdt1.rdType = ReaderTypeDAL.Add(rdt1); string old_rdTypeName = rdt1.rdTypeName; rdt1.rdTypeName = "管理员"; Console.WriteLine("DAL.ReaderType:测试Update()...rdType由" + old_rdTypeName + "改为" + rdt1.rdTypeName + "\n"); ReaderTypeDAL.Update(rdt1); #endregion #region DAL.Reader测试 Console.WriteLine("Model.Reader:测试创建数据单元..."); Reader rd1 = new Reader(); rd1.rdID = 1; rd1.rdUsername = "******"; rd1.rdPassword = "******"; rd1.rdName = "duguying"; rd1.rdSex = "男"; rd1.rdType = 1; rd1.rdDept = "计科院"; rd1.rdPhone = "18900000000"; rd1.rdEmail = "*****@*****.**"; rd1.rdDateReg = DateTime.Now; rd1.rdPhoto = Encoding.Default.GetBytes("test"); rd1.rdStatus = "正常"; rd1.rdHaveBorrowNum = 1; Console.WriteLine("DAL.Reader:测试Delete()..."); ReaderDAL.Delete(rd1); Console.WriteLine("DAL.Reader:测试Add()..."); rd1.rdID = ReaderDAL.Add(rd1); string old_rdPassword = rd1.rdPassword; rd1.rdPassword = "******"; Console.WriteLine("DAL.Reader:测试Update()...rdPassword由" + old_rdPassword + "改为" + rd1.rdPassword + "\n"); ReaderDAL.Update(rd1); #endregion #region DAL.Borrow测试 Console.WriteLine("Model.Borrow:测试创建数据单元..."); Borrow br1 = new Borrow(); br1.borrowId = 1; br1.bkId = 1; br1.ldContinueTimes = 0; br1.ldDateOut = DateTime.Now; br1.ldDateRetPlan = DateTime.Now; br1.ldDateRetAct = DateTime.Now; br1.ldOverDay = 12; br1.ldOverMoney = 1.2f; br1.IsHasReturn = false; br1.OperatorLendId = 1; br1.OperatorRetId = 0; Console.WriteLine("DAL.Borrow:测试Delete()..."); BorrowDAL.Delete(br1); Console.WriteLine("DAL.Borrow:测试Add()..."); br1.borrowId = (decimal)BorrowDAL.Add(br1); float old_ldPunishMoney = br1.ldPunishMoney; br1.ldPunishMoney = 2.4f; Console.WriteLine("DAL.Borrow:测试Update()...ldPunishMoney由" + old_ldPunishMoney + "改为" + br1.ldPunishMoney + "\n"); BorrowDAL.Update(br1); #endregion #region DAL.Book测试 Console.WriteLine("Model.Book:测试创建数据单元..."); Book bk1 = new Book(); bk1.bkId = 1; bk1.bkCode = "1234"; bk1.bkName = "测试书籍"; bk1.bkAuthor = "作者"; bk1.bkPress = "出版社"; bk1.bkDatePress = DateTime.Now; bk1.bkISBN = "92520111234"; bk1.bkCatalog = "计算机科学"; bk1.bkLanguage = 0; bk1.bkPages = 300; bk1.bkPrice = 30.5f; bk1.bkDateIn = DateTime.Now; bk1.bkBrief = "Go语言基础教程"; bk1.bkCover = Encoding.Default.GetBytes("test"); bk1.bkStatus = "在馆"; Console.WriteLine("DAL.Book:测试Delete()..."); BookDAL.Delete(bk1); Console.WriteLine("DAL.Book:测试Add()..."); bk1.bkId = BookDAL.Add(bk1); string old_bkName = bk1.bkName; bk1.bkName = "Go语言云动力"; Console.WriteLine("DAL.Book:测试Update()...bkName由" + old_bkName + "改为" + bk1.bkName + "\n"); BookDAL.Update(bk1); #endregion #region DAL.Admin测试 Console.WriteLine("Model.Admin:测试创建数据单元..."); Admin ad1 = new Admin(); ad1.adminId = 1; ad1.adminUsername = "******"; ad1.adminPassword = "******"; ad1.adminEmail = "*****@*****.**"; ad1.adminLastLoginDate = DateTime.Now; Console.WriteLine("DAL.Admin:测试Delete()..."); AdminDAL.Delete(ad1); Console.WriteLine("DAL.Admin:测试Add()..."); ad1.adminId = AdminDAL.Add(ad1); string old_adminPassword = ad1.adminPassword; ad1.adminPassword = "******"; Console.WriteLine("DAL.Admin:测试Update()...adminPassword由" + old_adminPassword + "改为" + ad1.adminPassword + "\n"); AdminDAL.Update(ad1); #endregion #region 测试数据清理 ReaderTypeDAL.Delete(rdt1); Console.WriteLine("ReaderType:数据已清理"); ReaderDAL.Delete(rd1); Console.WriteLine("Reader:数据已清理"); BorrowDAL.Delete(br1); Console.WriteLine("Borrow:数据已清理"); BookDAL.Delete(bk1); Console.WriteLine("Book:数据已清理"); AdminDAL.Delete(ad1); Console.WriteLine("Admin:数据已清理\n"); #endregion #region DAL.Admin扩展操作测试 AdminDAL.getAdminByUsername("lijun"); #endregion }
public static Int32 SubmitAction(UserAction action) { switch (action.actionSource) { case "ReaderType": switch (action.actionType) { case "Add": try { return(ReaderTypeDAL.Add((ReaderType)action.actionModel)); } catch (Exception) { return(0); } case "Update": try { return(ReaderTypeDAL.Update((ReaderType)action.actionModel)); } catch (Exception) { return(0); } case "Delete": try { return(ReaderTypeDAL.Delete((ReaderType)action.actionModel)); } catch (Exception) { return(0); } default: throw new Exception("Error actionType!"); } case "Reader": switch (action.actionType) { case "Add": try { return(ReaderDAL.Add((Reader)action.actionModel)); } catch (Exception) { return(0); } case "Update": try { return(ReaderDAL.Update((Reader)action.actionModel)); } catch (Exception) { return(0); } case "Delete": try { return(ReaderDAL.Delete((Reader)action.actionModel)); } catch (Exception) { return(0); } case "Password": try { return(ReaderDAL.Password((Reader)action.actionModel)); } catch (Exception) { return(0); } case "Permission": try { return(ReaderDAL.Permission((Reader)action.actionModel)); } catch (Exception) { return(0); } default: throw new Exception("Error actionType!"); } case "Book": switch (action.actionType) { case "Add": try { return(BookDAL.Add((Book)action.actionModel)); } catch (Exception) { return(0); } case "Update": try { return(BookDAL.Update((Book)action.actionModel)); } catch (Exception) { return(0); } case "Delete": try { return(BookDAL.Delete((Book)action.actionModel)); } catch (Exception) { return(0); } default: throw new Exception("Error actionType!"); } case "Borrow": switch (action.actionType) { case "Borrow": try { return(BorrowDAL.Borrow((Borrow)action.actionModel)); } catch (Exception) { return(0); } case "Update": try { return(BorrowDAL.Update((Borrow)action.actionModel)); } catch (Exception) { return(0); } case "Delete": try { return(BorrowDAL.Delete((Borrow)action.actionModel)); } catch (Exception) { return(0); } case "Return": try { return(BorrowDAL.Return((Borrow)action.actionModel)); } catch (Exception) { return(0); } case "Continue": try { return(BorrowDAL.Continue((Borrow)action.actionModel)); } catch (Exception) { return(0); } default: throw new Exception("Error actionType!"); } default: throw (new Exception("Error actionSource!")); } }
/// <summary> /// 增加 /// </summary> /// <param name="Book">Book实体对象</param> /// <returns>bool值,判断是否操作成功</returns> public bool Add(Book model) { return(dal.Add(model)); }