...
Codeblock | ||||||
---|---|---|---|---|---|---|
| ||||||
// GET: User/Create (Methode zum laden des Views) public ActionResult Create() { return View(); } // POST: User/Create (Methode zum Schreiben in die Datenbank) [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Name")]User user) { // Datenbankverbindung öffnen SQLiteConnection connection = DbConnect(); using (var command = new SQLiteCommand(connection)) { command.CommandText = string.Format(INSERT INTO db_User (Name,LastName) VALUES ('{0}','{1}')",user.Id,user.Name); command.ExecuteNonQuery(); } // Datenbankverbindung trennen DbDisconnect(connection); return RedirectToAction("Index"); } |
Codeblock | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
public ActionResult Index()
{
// Datenbankverbindung öffnen
SQLiteConnection connection = DbConnect();
// Read DB Content
List<User> users = new List<User>();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = "SELECT * FROM db_User";
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
users.Add(new User
{
Id = reader.GetInt32(0),
FirstName = reader.GetString(1)
});
}
reader.Close()
}
}
DbDisconnect(connection);
// Rückgabe der gelesenen Benutzer als Liste
return View(users.ToList());
} |