projektAutoHandlung/WpfApp4/DatabaseService.cs
2025-08-31 23:33:31 +02:00

204 lines
8.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
namespace FahrzeugVerwaltung
{
public class DatabaseService
{
private readonly string _connectionString;
private readonly string _dbPath;
public DatabaseService()
{
_dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fahrzeuge.db");
_connectionString = $"Data Source={_dbPath};Version=3;";
ErstelleDatenbank();
}
// Legt Datenbank und Tabelle an
private void ErstelleDatenbank()
{
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = @"
CREATE TABLE IF NOT EXISTS Fahrzeuge (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Marke TEXT NOT NULL,
Modell TEXT NOT NULL,
Baujahr INTEGER NOT NULL,
Leistung INTEGER NOT NULL,
Kilometerstand INTEGER NOT NULL,
Kaufpreis DECIMAL(10,2) NOT NULL,
Farbe TEXT NOT NULL,
ErstelltAm DATETIME DEFAULT CURRENT_TIMESTAMP
)";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
}
// Speichert ein Fahrzeug und liefert die Id
public int SpeichereFahrzeug(Fahrzeug fahrzeug)
{
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = @"
INSERT INTO Fahrzeuge (Marke, Modell, Baujahr, Leistung, Kilometerstand, Kaufpreis, Farbe)
VALUES (@Marke, @Modell, @Baujahr, @Leistung, @Kilometer, @Kaufpreis, @Farbe);
SELECT last_insert_rowid();";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@Marke", fahrzeug.Marke);
command.Parameters.AddWithValue("@Modell", fahrzeug.Modell);
command.Parameters.AddWithValue("@Baujahr", fahrzeug.Baujahr);
command.Parameters.AddWithValue("@Leistung", fahrzeug.Leistung);
command.Parameters.AddWithValue("@Kilometer", fahrzeug.Kilometerstand);
command.Parameters.AddWithValue("@Kaufpreis", fahrzeug.Kaufpreis);
command.Parameters.AddWithValue("@Farbe", fahrzeug.Farbe);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
// Liefert alle Fahrzeuge
public List<Fahrzeug> LadeAlleFahrzeuge()
{
List<Fahrzeug> fahrzeuge = new List<Fahrzeug>();
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = "SELECT * FROM Fahrzeuge ORDER BY Marke, Modell";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
fahrzeuge.Add(new Fahrzeug
{
Id = Convert.ToInt32(reader["Id"]),
Marke = reader["Marke"].ToString(),
Modell = reader["Modell"].ToString(),
Baujahr = Convert.ToInt32(reader["Baujahr"]),
Leistung = Convert.ToInt32(reader["Leistung"]),
Kilometerstand = Convert.ToInt32(reader["Kilometerstand"]),
Kaufpreis = Convert.ToDecimal(reader["Kaufpreis"]),
Farbe = reader["Farbe"].ToString()
});
}
}
}
return fahrzeuge;
}
// Sucht Fahrzeuge nach Text
public List<Fahrzeug> SucheFahrzeuge(string suchbegriff)
{
if (string.IsNullOrWhiteSpace(suchbegriff))
{
return LadeAlleFahrzeuge();
}
List<Fahrzeug> fahrzeuge = new List<Fahrzeug>();
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = @"
SELECT * FROM Fahrzeuge
WHERE Marke LIKE @S
OR Modell LIKE @S
OR Farbe LIKE @S
OR CAST(Baujahr AS TEXT) LIKE @S
ORDER BY Marke, Modell";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@S", $"%{suchbegriff}%");
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
fahrzeuge.Add(new Fahrzeug
{
Id = Convert.ToInt32(reader["Id"]),
Marke = reader["Marke"].ToString(),
Modell = reader["Modell"].ToString(),
Baujahr = Convert.ToInt32(reader["Baujahr"]),
Leistung = Convert.ToInt32(reader["Leistung"]),
Kilometerstand = Convert.ToInt32(reader["Kilometerstand"]),
Kaufpreis = Convert.ToDecimal(reader["Kaufpreis"]),
Farbe = reader["Farbe"].ToString()
});
}
}
}
}
return fahrzeuge;
}
// Aktualisiert ein Fahrzeug
public bool AktualisiereFahrzeug(Fahrzeug fahrzeug)
{
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = @"
UPDATE Fahrzeuge
SET Marke=@Marke,
Modell=@Modell,
Baujahr=@Baujahr,
Leistung=@Leistung,
Kilometerstand=@Kilometer,
Kaufpreis=@Kaufpreis,
Farbe=@Farbe
WHERE Id=@Id";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@Id", fahrzeug.Id);
command.Parameters.AddWithValue("@Marke", fahrzeug.Marke);
command.Parameters.AddWithValue("@Modell", fahrzeug.Modell);
command.Parameters.AddWithValue("@Baujahr", fahrzeug.Baujahr);
command.Parameters.AddWithValue("@Leistung", fahrzeug.Leistung);
command.Parameters.AddWithValue("@Kilometer", fahrzeug.Kilometerstand);
command.Parameters.AddWithValue("@Kaufpreis", fahrzeug.Kaufpreis);
command.Parameters.AddWithValue("@Farbe", fahrzeug.Farbe);
return command.ExecuteNonQuery() > 0;
}
}
}
// Löscht ein Fahrzeug
public bool LoescheFahrzeug(int id)
{
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = "DELETE FROM Fahrzeuge WHERE Id=@Id";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.Parameters.AddWithValue("@Id", id);
return command.ExecuteNonQuery() > 0;
}
}
}
// Zählt vorhandene Fahrzeuge
public int HoleAnzahlFahrzeuge()
{
using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
{
connection.Open();
string query = "SELECT COUNT(*) FROM Fahrzeuge";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
}
}