//Method used for saving rates into the database public bool saveRate(RateClass rate) { bool Successful = false; CurrencyClass ccFrom = rate.getFrom(); CurrencyClass ccTo = rate.getTo(); //If entry does not exist insert it if (checkForDuplicate(rate) == false) { try { string insert = "INSERT INTO CurrencyConverter ([CurFromLong], [CurFromShort], [CurToLong], [CurToShort], [Rate], [DateTime]) VALUES (@CurFromLong, @CurFromShort, @CurToLong, @CurToShort, @Rate, @DateTime);"; OleDbCommand cmd = new OleDbCommand(insert, MyConn); cmd.Parameters.AddWithValue("@CurFromLong", ccFrom.getLongName()); cmd.Parameters.AddWithValue("@CurFromShort", ccFrom.getShortName()); cmd.Parameters.AddWithValue("@CurToLong", ccTo.getLongName()); cmd.Parameters.AddWithValue("@CurToShort", ccTo.getShortName()); cmd.Parameters.AddWithValue("@Rate", rate.getRate().ToString()); cmd.Parameters.AddWithValue("@DateTime", rate.getTimeDate().ToString()); cmd.ExecuteNonQuery(); Successful = true; } //endtry catch {} } //endif //if entry does exist, update it else { try { string update = "UPDATE CurrencyConverter SET [Rate]=@Rate, [DateTime]=@DateTime WHERE CurFromShort LIKE '" + ccFrom.getShortName() + "' AND CurToShort LIKE '" + ccTo.getShortName() + "'"; OleDbCommand cmd = new OleDbCommand(update, MyConn); cmd.Parameters.AddWithValue("@Rate", rate.getRate().ToString()); cmd.Parameters.AddWithValue("@DateTime", rate.getTimeDate().ToString()); cmd.ExecuteNonQuery(); Successful = true; }//endtry catch { //returns false } //endcatch } //endelse return(Successful); } //Saverate
} //OpenDatabaseConnection //Checks to see if provided entry already exists in the Database private bool checkForDuplicate(RateClass rate) { try { CurrencyClass ccFrom = rate.getFrom(); CurrencyClass ccTo = rate.getTo(); string CommandString = "Select * from CurrencyConverter where CurFromShort like '" + ccFrom.getShortName() + "' and CurToShort like '" + ccTo.getShortName() + "'"; OleDbCommand cmd = new OleDbCommand(CommandString, MyConn); OleDbDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { return(true); } else { return(false); } } catch { return(false); } } //checkForDuplicate