...
Codeblock | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// GET: User/Edit/5 (View laden und mit Informationen aus der Datenbank befüllen) public ActionResult Edit(int? id) { User user; // Falls ID nicht vorhanden -> Error if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // Datenbankverbindung öffnen SQLiteConnection connection = DbConnect(); using (var command = new SQLiteCommand(connection)) { command.CommandText = string.Format("SELECT * FROM db_User WHERE Id = " + id); using (SQLiteDataReader reader = command.ExecuteReader()) { reader.Read(); user = new User { Id = reader.GetInt32(0), Name = reader.GetString(1) }; } } // Informationen über User an den View übergeben return View(user) } // POST: User/Edit/5 (Aktualisierte Daten in Datenbank schreiben) [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,Name")]User user) { SQLiteConnection connection = DbConnect(); using (var command = new SQLiteCommand(connection)) { command.CommandText = string.Format("UPDATE db_User SET Name = '{1}' WHERE Id = {0}", user.Id, user.Name ); command.ExecuteNonQuery(); } return RedirectToAction("Index"); } |
Codeblock | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// GET: User/Delete/1 (View für Löschbestätigung laden)
public ActionResult Delete(int? id)
{
User user;
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SQLiteConnection connection = DbConnect();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = string.Format("SELECT * FROM db_User WHERE Id = " + id);
using (SQLiteDataReader reader = command.ExecuteReader())
{
reader.Read();
user = new User
{
Id = reader.GetInt32(0),
Name = reader.GetString(1)
};
}
}
return View(user);
}
// POST: User/Delete/1 (Methode zum löschen eines Datensatzes in der Datenbank)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete([Bind(Include = "Id,Name")]User user)
{
SQLiteConnection connection = DbConnect();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = string.Format("DELETE FROM db_User WHERE id = {0}", user.Id);
command.ExecuteNonQuery();
}
return RedirectToAction("Index");
} |