public void ReturnBook(int checkoutId) { Checkout targetCheckout = Checkout.Find(checkoutId); Book targetBook = Book.Find(targetCheckout.bookId); SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("UPDATE checkouts SET returned = 1 WHERE id = @checkoutId; UPDATE books SET checked_in = @checkedIn, checked_out = @checkedOut WHERE id = @bookId;", conn); cmd.Parameters.AddWithValue("@checkoutId", targetCheckout.id); cmd.Parameters.AddWithValue("@bookId", targetCheckout.bookId); cmd.Parameters.AddWithValue("@checkedIn", targetBook.checkedIn + 1); cmd.Parameters.AddWithValue("@checkedOut", targetBook.checkedOut - 1); cmd.ExecuteNonQuery(); if (conn != null) { conn.Close(); } }
public static Checkout Find(int id) { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM checkouts WHERE id = @checkoutId;", conn); cmd.Parameters.AddWithValue("@checkoutId", id); SqlDataReader rdr = cmd.ExecuteReader(); int idNew = 0; int bookIdNew = 0; int memberIdNew = 0; DateTime dueDateNew = DateTime.Today; bool returnedNew = false; while (rdr.Read()) { idNew = rdr.GetInt32(0); bookIdNew = rdr.GetInt32(1); memberIdNew = rdr.GetInt32(2); dueDateNew = rdr.GetDateTime(3); returnedNew = rdr.GetBoolean(4); } Checkout foundCheckout = new Checkout(bookIdNew, memberIdNew, dueDateNew, returnedNew, idNew); if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return(foundCheckout); }