//2.2 public static int Update(Hriste hriste, Database pDb = null) { Database db; if (pDb == null) { db = new Database(); db.Connect(); } else { db = pDb; } using (db) using (SqlCommand command = db.CreateCommand(SQL_UPDATE)) { command.Parameters.AddWithValue("@idHriste", hriste.IdHriste); PrepareCommand(command, hriste); int status = db.ExecuteNonQuery(command); if (pDb == null) { db.Close(); } return status; } }
//2.1 public static int Insert(Hriste hriste, Database pDb = null) { Database db; if (pDb == null) { db = new Database(); db.Connect(); } else { db = pDb; } using (db) using (SqlCommand command = db.CreateCommand(SQL_INSERT)) { PrepareCommand(command, hriste); int status = db.ExecuteNonQuery(command); if (pDb == null) { db.Close(); } return status; } }
private static Collection<Hriste> Read(SqlDataReader reader) { var hriste = new Collection<Hriste>(); while (reader.Read()) { int i = -1; Hriste hr = new Hriste() { IdHriste = reader.GetInt32(++i), Misto = reader.GetString(++i), Povrch = reader.GetString(++i) }; hriste.Add(hr); } return hriste; }
private static void PrepareCommand(SqlCommand command, Hriste hriste) { command.Parameters.AddWithValue("@misto", hriste.Misto); command.Parameters.AddWithValue("@povrch", hriste.Povrch); }