projektAutoHandlung/WpfApp4/DatabaseService.cs
2025-08-31 17:11:58 +02:00

172 lines
7.2 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;";
InitializeDatabase();
}
// erstellt die Datenbank und Tabelle
private void InitializeDatabase()
{
using var 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 var command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();
}
// speichert ein Fahrzeug und gibt die neue Id zurück
public int SpeichereFahrzeug(Fahrzeug fahrzeug)
{
using var 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 var 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());
}
// lädt alle Fahrzeuge
public List<Fahrzeug> LadeAlleFahrzeuge()
{
var fahrzeuge = new List<Fahrzeug>();
using var connection = new SQLiteConnection(_connectionString);
connection.Open();
string query = "SELECT * FROM Fahrzeuge ORDER BY Marke, Modell";
using var command = new SQLiteCommand(query, connection);
using var 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 Begriff
public List<Fahrzeug> SucheFahrzeuge(string suchbegriff)
{
if (string.IsNullOrWhiteSpace(suchbegriff))
return LadeAlleFahrzeuge();
var fahrzeuge = new List<Fahrzeug>();
using var 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 var command = new SQLiteCommand(query, connection);
command.Parameters.AddWithValue("@S", $"%{suchbegriff}%");
using var 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 var 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 var 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 var connection = new SQLiteConnection(_connectionString);
connection.Open();
string query = "DELETE FROM Fahrzeuge WHERE Id=@Id";
using var command = new SQLiteCommand(query, connection);
command.Parameters.AddWithValue("@Id", id);
return command.ExecuteNonQuery() > 0;
}
// gibt die Anzahl der Fahrzeuge zurück
public int GetAnzahlFahrzeuge()
{
using var connection = new SQLiteConnection(_connectionString);
connection.Open();
string query = "SELECT COUNT(*) FROM Fahrzeuge";
using var command = new SQLiteCommand(query, connection);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}