Add LoadData from DB functionality

This commit is contained in:
Francesco 2020-07-28 19:07:35 +02:00
parent c661fd8d79
commit 965b8a34dd

View File

@ -1,5 +1,8 @@
using LoggingClient.Model; using LoggingClient.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows;
namespace LoggingClient.Repository namespace LoggingClient.Repository
{ {
@ -7,16 +10,19 @@ namespace LoggingClient.Repository
{ {
public CustomerRepository(string connectionString) : base(connectionString) public CustomerRepository(string connectionString) : base(connectionString)
{ {
Customers = new List<Customer>();
} }
public override string TableName => throw new System.NotImplementedException(); public override string TableName => "customer";
public override string ColumnsForSelect => throw new System.NotImplementedException(); public override string ColumnsForSelect => "customer_id, firstname, lastname, customernumber, kundenkonto_fk, tel, email, url, password";
public override string ColumnsForAdd => throw new System.NotImplementedException(); public override string ColumnsForAdd => "firstname, lastname, customernumber, kundenkonto_fk, tel, email, url, password";
public override string PrimaryKeyFromTable => throw new System.NotImplementedException(); public override string PrimaryKeyFromTable => "customer_id";
public List<Customer> Customers { get; set; }
public Customer _Customers { get; set; }
public override void Add(Customer entity) public override void Add(Customer entity)
{ {
@ -40,7 +46,37 @@ namespace LoggingClient.Repository
public override List<Customer> GetAll() public override List<Customer> GetAll()
{ {
throw new System.NotImplementedException(); try
{
using (var conn = new MySqlConnection(ConnectionString))
{
conn.Open();
using (MySqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName}";
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Customers.Add(new Customer(
reader.GetInt32("customer_id"),
reader.GetValue(reader.GetOrdinal("firstname")) as string,
reader.GetValue(reader.GetOrdinal("lastname")) as string,
reader.GetValue(reader.GetOrdinal("customernumber")) as string,
reader.GetInt32("kundenkonto_fk"),
reader.GetValue(reader.GetOrdinal("tel")) as string,
reader.GetValue(reader.GetOrdinal("email")) as string,
reader.GetValue(reader.GetOrdinal("url")) as string,
reader.GetValue(reader.GetOrdinal("password")) as string
));
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error occurred: " + ex.Message);
}
return Customers;
} }
public override Customer GetSingle<P>(P pkValue) public override Customer GetSingle<P>(P pkValue)