public void updateMoney(UInt64 id, MoneyInfo mInfo) { if (!connect()) return; try { NpgsqlCommand command = new NpgsqlCommand("select count(*) from money where id = :id", connection); command.Parameters.Add(new NpgsqlParameter("id", NpgsqlTypes.NpgsqlDbType.Numeric)); command.Parameters["id"].Value = id; NpgsqlDataReader rd = command.ExecuteReader(); UInt64 count = 0; while (rd.Read()) { count = Convert.ToUInt64(rd[0]); } String query; if (count == 0) { query = "insert into money (id, balance, pin, failures) values (:id, :b, :p, :f)"; } else { query = "update money set balance = :b, pin = :p, failures = :f where id = :id"; } command = new NpgsqlCommand(query, connection); command.Parameters.Add(new NpgsqlParameter("id", NpgsqlTypes.NpgsqlDbType.Numeric)); command.Parameters["id"].Value = id; command.Parameters.Add(new NpgsqlParameter("b", NpgsqlTypes.NpgsqlDbType.Numeric)); command.Parameters["b"].Value = mInfo.balance; command.Parameters.Add(new NpgsqlParameter("p", NpgsqlTypes.NpgsqlDbType.Varchar)); command.Parameters["p"].Value = mInfo.pinCode; command.Parameters.Add(new NpgsqlParameter("f", NpgsqlTypes.NpgsqlDbType.Numeric)); command.Parameters["f"].Value = mInfo.failures; command.ExecuteNonQuery(); } catch (Exception ex) { HandleException(ex); } finally { disconnect(); } }
public MoneyInfo getMoneyInfo(UInt64 id) { if (!connect()) return null; MoneyInfo ret = new MoneyInfo(); NpgsqlCommand command = new NpgsqlCommand("select BALANCE, PIN, FAILURES from MONEY where ID = :value1 ORDER BY ID ASC", connection); try { command.Parameters.Add(new NpgsqlParameter("value1", NpgsqlTypes.NpgsqlDbType.Numeric)); command.Parameters[0].Value = id; NpgsqlDataReader rd = command.ExecuteReader(); while (rd.Read()) { ret.balance = Convert.ToUInt64(rd["BALANCE"]); ret.pinCode = Convert.ToString(rd["PIN"]); ret.failures = Convert.ToUInt16(rd["FAILURES"]); } } catch (Exception ex) { HandleException(ex); } finally { disconnect(); } return ret; }