//删除一本书 //0:删除失败 //1:删除成功 //2:连接出错,删除失败 //3:书本不存在,删除失败 public int delete(string id) { //判断连接 if (DbHelper.checkConnection() == false) { return(2); } string cmdQueryText = "select * from book where b_id='" + id + "'"; Object obj = DbHelper.ExecuteScalar(cmdQueryText); if (obj == null) { Console.WriteLine("书本不存在"); return(3); } else { string cmdText = "delete from book where b_id=" + id; int i = DbHelper.ExecuteNonQuery(cmdText); if (i == 1) { return(1); } else { Console.WriteLine("删除失败"); return(0); } } }
//增加一本书 //0:增加失败 //1:增加成功 //2:连接出错,增加失败 //3:图片为空,增加失败 public int insert(Model.Book book) { string name = book.Name; string author = book.Author; string publisher = book.Publisher; string pubtime = book.Pubtime; string pritime = book.Pritime; string edition = book.Edition; string impression = book.Impression; string pages = book.Pages; string words = book.Words; string format = book.Format; string paper = book.Paper; string packaging = book.Packaging; string isbn = book.Isbn; byte[] picture = book.Picture; string price = book.Price; string sort = book.Sort; string inventory = book.Inventory; //判断连接 if (DbHelper.checkConnection() == false) { return(2); } //判断图片 if (picture == null) { return(3); } string cmdText = "insert into book(b_name,b_author,b_publisher,b_pubtime,b_pritime,b_edition,b_impression,b_pages,b_words,b_format,b_paper,b_packaging,b_isbn,b_picture,b_price,b_sort,b_inventory)values('" + name + "','" + author + "','" + publisher + "','" + pubtime + "','" + pritime + "','" + edition + "','" + impression + "','" + pages + "','" + words + "','" + format + "','" + paper + "','" + packaging + "','" + isbn + "',@picture,'" + price + "','" + sort + "','" + inventory + "')"; SqlParameter[] parameters = { new SqlParameter("@picture", SqlDbType.Binary) }; parameters[0].Value = picture; int i = DbHelper.ExecuteNonQuery(cmdText, parameters); if (i == 1) { return(1); } else { Console.WriteLine("增加失败"); return(0); } }
//判断数据库是否可以连接 public bool checkConnection() { return(DbHelper.checkConnection()); }
//更新一本书 //0:更新失败 //1:更新成功 //2:连接出错,更新失败 //3:图片为空,更新失败 //4:ID为空,更新失败 // public int update(Model.Book book) { string id = book.Id; string name = book.Name; string author = book.Author; string publisher = book.Publisher; string pubtime = book.Pubtime; string pritime = book.Pritime; string edition = book.Edition; string impression = book.Impression; string pages = book.Pages; string words = book.Words; string format = book.Format; string paper = book.Paper; string packaging = book.Packaging; string isbn = book.Isbn; byte[] picture = book.Picture; string price = book.Price; string sort = book.Sort; string inventory = book.Inventory; //判断连接 if (DbHelper.checkConnection() == false) { return(2); } if (picture == null) { return(3); } if (id == null || id == "") { return(4); } string cmdText = "update book set b_name='" + name + "',b_author='" + author + "',b_publisher='" + publisher + "',b_pubtime='" + pubtime + "',b_pritime='" + pritime + "',b_edition='" + edition + "',b_impression='" + impression + "',b_pages='" + pages + "',b_words='" + words + "',b_format='" + format + "',b_paper='" + paper + "',b_packaging='" + packaging + "',b_isbn='" + isbn + "',b_picture=@picture,b_price='" + price + "',b_sort='" + sort + "',b_inventory='" + inventory + "' where b_id='" + id + "'"; SqlParameter[] parameters = { new SqlParameter("@picture", SqlDbType.Binary) }; parameters[0].Value = picture; int i = DbHelper.ExecuteNonQuery(cmdText, parameters); if (i == 1) { return(1); } else { Console.WriteLine("更新失败"); return(0); } }