/// <summary> /// Updates an item with specified ID /// </summary> /// <returns>ID of updated object as Int, 0 if an object with specified ID doesn't exist.</returns> public static int updateRockAndRoll(int id, RockAndRoll rnr) { SqlConnection con = new SqlConnection(_ConnectionString); SqlCommand cmd = new SqlCommand("UPDATE Rockandroll SET Title='" + rnr.Title + "', Artist='" + rnr.Artist + "', Album='" + rnr.Album + "', Year='" + rnr.Year + "', Link='" + rnr.Link + "'WHERE RockAndRollID='" + id.ToString() + "'", con); RockAndRoll oldRnr = getRockAndRoll(id); int returnID = 0; using (con) { try { con.Open(); returnID = (int)cmd.ExecuteScalar(); } catch (Exception ex) { LastError = ex.Message; } } return returnID; }
/// <summary> /// Gets a single RockAndRoll item with specified ID. /// </summary> /// <param name="id"></param> /// <returns>RockAndRoll object or null</returns> public static RockAndRoll getRockAndRoll(int id) { SqlConnection con = new SqlConnection(_ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * FROM Rockandroll WHERE RockAndRollID='" + id.ToString() + "'", con); RockAndRoll rnr = null; using (con) { try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); rnr = new RockAndRoll((string)reader["Title"], (string)reader["Artist"], (string)reader["Album"], (int)reader["Year"], (string)reader["Link"]); } catch (System.Exception ex) { LastError = ex.Message; } } return rnr; }
/// <summary> /// Inserts new RockAndRoll object into database. /// </summary> /// <returns>ID of the new object as int</returns> public static int insertNewRockAndRoll(RockAndRoll rnr) { SqlConnection con = new SqlConnection(_ConnectionString); SqlCommand cmd = new SqlCommand("INSERT into Rockandroll (Title, Artist, Album, Year, Link) VALUES('" + rnr.Title + "', '" + rnr.Artist + "', '" + rnr.Album + "', '" + rnr.Year + "', '" + rnr.Link + "'); " + "SELECT CAST(Scope_Identity() as int)", con); Int32 returnID = 0; using (con) { try { con.Open(); returnID = (Int32)cmd.ExecuteScalar(); // Why this always returns 0? } catch (Exception ex) { LastError = ex.Message; } } return returnID; }
/// <summary> /// Gets all the RockAndRoll from the database. /// </summary> /// <returns>List of all sex items</returns> public static List<RockAndRoll> getAllRockAndRoll() { List<RockAndRoll> rnrList = new List<RockAndRoll>(); SqlConnection con = new SqlConnection(_ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * FROM Rockandroll", con); using (con) { try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { RockAndRoll r = new RockAndRoll((string)reader["Title"], (string)reader["Artist"], (string)reader["Album"], (int)reader["Year"], (string)reader["Link"]); rnrList.Add(r); } } catch (System.Exception ex) { LastError = ex.Message; } } return rnrList; }