public void AddBookToDB(_Book book, string linkPhoto) { using (SqlConnection connection = new SqlConnection(ConnectionStrings.DBQuery)) { using (SqlCommand cmd = new SqlCommand(ConnectionStrings.AddBook, connection)) { try { connection.Open(); cmd.Parameters.AddWithValue("@BookName", book.BookName); cmd.Parameters.AddWithValue("@Authors", book.Authors); cmd.Parameters.AddWithValue("@Total", book.Total); cmd.Parameters.AddWithValue("@Available", book.Total); cmd.Parameters.AddWithValue("@LinkPhoto", linkPhoto); cmd.ExecuteNonQuery(); } catch (Exception ex) { System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/App_Data/ErrorLogDataBase.txt"), true); writer.WriteLine(string.Format("Date : " + DateTime.Now.ToString() + " Error : " + ex.Message)); writer.Close(); } } } }
//Получить доступные книги public BooksContainer AllAvailable() { booksContainer = new BooksContainer(); using (SqlConnection connection = new SqlConnection(ConnectionStrings.DBQuery)) { SqlCommand command = new SqlCommand(ConnectionStrings.BooksAvailable, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { book = new _Book(); book.BookName = reader[0].ToString(); book.Authors = reader[1].ToString(); book.Total = int.Parse(reader[2].ToString()); book.Available = int.Parse(reader[3].ToString()); book.LinkPhoto = reader[5].ToString(); booksContainer.Add(book); } reader.Close(); return(booksContainer); } catch (Exception ex) { System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/App_Data/ErrorLogDataBase.txt"), true); writer.WriteLine(string.Format("Date : " + DateTime.Now.ToString() + " Error : " + ex.Message)); writer.Close(); return(null); } } }
//считываем из базы наименования книг public BooksContainer BooksReading() { booksContainer = new BooksContainer(); using (SqlConnection connection = new SqlConnection(ConnectionStrings.DBQuery)) { SqlCommand command = new SqlCommand(ConnectionStrings.BooksQuery, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { book = new _Book(); book.BookName = reader[0].ToString(); book.Authors = reader[1].ToString(); book.Total = int.Parse(reader[2].ToString()); book.Available = int.Parse(reader[3].ToString()); book.Id = int.Parse(reader[4].ToString()); book.LinkPhoto = reader[5].ToString(); booksContainer.Add(book); } reader.Close(); return(booksContainer); } catch (Exception ex) { //throw new Exception(ex.Message); Console.WriteLine(ex.Message); return(null); } } }
public ActionResult Upload(HttpPostedFileBase upload, _Book book) { if (upload != null) { try { string fileName = System.IO.Path.GetFileName(upload.FileName); string linkPhoto = string.Format("~/Resources/Photo/" + fileName); upload.SaveAs(Server.MapPath(linkPhoto)); AddBookToDB(book, linkPhoto); } catch (Exception ex) { System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/App_Data/ErrorLogDataBase.txt"), true); writer.WriteLine(string.Format("Date : " + DateTime.Now.ToString() + " Error : " + ex.Message)); writer.Close(); } } return(RedirectToAction("IndexAdm")); }