Erweiterung mit Kommentaren
This commit is contained in:
parent
6a05c7b75b
commit
5c51f68e6c
@ -29,15 +29,267 @@
|
||||
// Der komplette Datensatz eines Nutzers darf dann nicht importiert werden
|
||||
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using MySql.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using MySqlX.XDevAPI;
|
||||
using System.Data.Common;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Xml;
|
||||
|
||||
/* Das hier wäre eine Lösungsvariante. Diese beruht auf den vorhandenen Daten
|
||||
*
|
||||
* Anmerkungen:
|
||||
* Passe den Pfad zur CSV-Datei (filePath) an.
|
||||
* Stelle sicher, dass deine Datenbankverbindung (myConnectionString) korrekt ist.
|
||||
* Die Tabellenstruktur in deiner MySQL-Datenbank sollte mit den Feldern im Code übereinstimmen.
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Verbindungszeichenfolge zur MySQL-Datenbank
|
||||
string myConnectionString = "server=localhost;uid=root;pwd=root;database=import_export";
|
||||
|
||||
// Willkommensnachricht und Eingabeaufforderung für Kundennummer und Dateiparameter
|
||||
Console.WriteLine("Willkommen");
|
||||
Console.Write("Bitte geben Sie nun die Kundennummer ein: ");
|
||||
string kundennummer = Console.ReadLine();
|
||||
Console.Write("Bitte geben Sie die Datei-Parameter ein: ");
|
||||
string dateiBezeichnung = Console.ReadLine();
|
||||
|
||||
// Vollständiger Pfad zur CSV-Datei
|
||||
string filePath = "path/to/your/" + dateiBezeichnung; // Passe den Pfad zur CSV-Datei an
|
||||
|
||||
// Überprüfen, ob die Datei existiert
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
Console.WriteLine("Die Datei-Parameter konnten nicht gefunden werden. Bitte versuche es erneut");
|
||||
return;
|
||||
}
|
||||
|
||||
// Listen für ungültige und vorhandene Einträge
|
||||
List<string> invalidEntries = new List<string>();
|
||||
List<string> existingEntries = new List<string>();
|
||||
|
||||
// Einlesen der CSV-Datei und Verarbeitung der Daten
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
List<User> users = ParseCsv(lines, invalidEntries);
|
||||
|
||||
// Verbindung zur MySQL-Datenbank öffnen
|
||||
using (MySqlConnection myConnection = new MySqlConnection(myConnectionString))
|
||||
{
|
||||
myConnection.Open();
|
||||
|
||||
// Für jeden Nutzer aus der CSV-Datei überprüfen und einfügen
|
||||
foreach (User user in users)
|
||||
{
|
||||
// Überprüfen, ob der Nutzer bereits existiert
|
||||
if (UserExists(myConnection, user, kundennummer))
|
||||
{
|
||||
existingEntries.Add($"{user.Firstname} {user.Lastname}, {user.Email}");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Nutzer einfügen, wenn er nicht existiert
|
||||
InsertUser(myConnection, user, kundennummer);
|
||||
}
|
||||
}
|
||||
|
||||
// Ungültige und vorhandene Einträge protokollieren
|
||||
LogEntries("invalid_entries.log", invalidEntries);
|
||||
LogEntries("existing_entries.log", existingEntries);
|
||||
|
||||
// Import abgeschlossen
|
||||
Console.WriteLine("Import abgeschlossen.");
|
||||
}
|
||||
|
||||
// Methode zum Parsen der CSV-Datei
|
||||
static List<User> ParseCsv(string[] lines, List<string> invalidEntries)
|
||||
{
|
||||
List<User> users = new List<User>();
|
||||
User currentUser = null;
|
||||
|
||||
// Zeilen der CSV-Datei durchgehen
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string[] parts = line.Split(';');
|
||||
|
||||
// Beginn eines neuen Nutzers (Herr/Frau)
|
||||
if (parts[0] == "Herr" || parts[0] == "Frau")
|
||||
{
|
||||
// Vorherigen Nutzer zur Liste hinzufügen, wenn vorhanden
|
||||
if (currentUser != null)
|
||||
{
|
||||
users.Add(currentUser);
|
||||
}
|
||||
// Neuen Nutzer erstellen
|
||||
currentUser = new User
|
||||
{
|
||||
Gender = parts[0],
|
||||
Firstname = parts[1],
|
||||
Lastname = parts[2],
|
||||
Birthdate = parts[3]
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
// Adresse des aktuellen Nutzers einlesen
|
||||
if (parts[0] == "Adresse" && currentUser != null)
|
||||
{
|
||||
currentUser.Address = new Address
|
||||
{
|
||||
Street = parts[1],
|
||||
PostalCode = parts[2],
|
||||
City = parts[3]
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
// E-Mail-Adresse des aktuellen Nutzers einlesen und validieren
|
||||
if (parts[0] == "E-Mail" && currentUser != null)
|
||||
{
|
||||
currentUser.Email = parts[1];
|
||||
if (!IsValidEmail(currentUser.Email))
|
||||
{
|
||||
invalidEntries.Add($"Ungültige E-Mail: {currentUser.Email}");
|
||||
currentUser = null;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Telefonnummer des aktuellen Nutzers einlesen und validieren
|
||||
if (parts[0] == "Telefon" && currentUser != null)
|
||||
{
|
||||
if (!IsValidPhone(parts[1], parts[2]))
|
||||
{
|
||||
invalidEntries.Add($"Ungültige Telefonnummer: {parts[1]} {parts[2]}");
|
||||
currentUser = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentUser.Phones.Add(new Phone
|
||||
{
|
||||
Prefix = parts[1],
|
||||
Number = parts[2]
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Letzten Nutzer zur Liste hinzufügen, falls vorhanden
|
||||
if (currentUser != null)
|
||||
{
|
||||
users.Add(currentUser);
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
// Methode zur Überprüfung, ob ein Nutzer bereits in der Datenbank existiert
|
||||
static bool UserExists(MySqlConnection connection, User user, string clientId)
|
||||
{
|
||||
string query = @"SELECT COUNT(*) FROM user WHERE firstname = @firstname AND lastname = @lastname AND clientId = @clientId";
|
||||
MySqlCommand cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@firstname", user.Firstname);
|
||||
cmd.Parameters.AddWithValue("@lastname", user.Lastname);
|
||||
cmd.Parameters.AddWithValue("@clientId", clientId);
|
||||
return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
|
||||
}
|
||||
|
||||
// Methode zum Einfügen eines neuen Nutzers in die Datenbank
|
||||
static void InsertUser(MySqlConnection connection, User user, string clientId)
|
||||
{
|
||||
string query = @"INSERT INTO user (clientId, genderId, firstname, lastname, birthdate) VALUES (@clientId, @genderId, @firstname, @lastname, @birthdate)";
|
||||
MySqlCommand cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@clientId", clientId);
|
||||
cmd.Parameters.AddWithValue("@genderId", user.Gender == "Herr" ? 1 : (user.Gender == "Frau" ? 2 : 3));
|
||||
cmd.Parameters.AddWithValue("@firstname", user.Firstname);
|
||||
cmd.Parameters.AddWithValue("@lastname", user.Lastname);
|
||||
cmd.Parameters.AddWithValue("@birthdate", DateTime.ParseExact(user.Birthdate, "dd.MM.yyyy", CultureInfo.InvariantCulture));
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
long userId = cmd.LastInsertedId;
|
||||
|
||||
// Adresse des Nutzers einfügen, falls vorhanden
|
||||
if (user.Address != null)
|
||||
{
|
||||
query = @"INSERT INTO address (userId, street, postalcode, city) VALUES (@userId, @street, @postalcode, @city)";
|
||||
cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@userId", userId);
|
||||
cmd.Parameters.AddWithValue("@street", user.Address.Street);
|
||||
cmd.Parameters.AddWithValue("@postalcode", user.Address.PostalCode);
|
||||
cmd.Parameters.AddWithValue("@city", user.Address.City);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// E-Mail-Adresse des Nutzers einfügen, falls vorhanden
|
||||
if (!string.IsNullOrEmpty(user.Email))
|
||||
{
|
||||
query = @"INSERT INTO email (userId, email) VALUES (@userId, @Email)";
|
||||
cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@userId", userId);
|
||||
cmd.Parameters.AddWithValue("@Email", user.Email);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// Telefonnummern des Nutzers einfügen, falls vorhanden
|
||||
foreach (var phone in user.Phones)
|
||||
{
|
||||
query = @"INSERT INTO phone (userId, phoneprefix, phonenumber) VALUES (@userId, @phoneprefix, @phonenumber)";
|
||||
cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@userId", userId);
|
||||
cmd.Parameters.AddWithValue("@phoneprefix", phone.Prefix);
|
||||
cmd.Parameters.AddWithValue("@phonenumber", phone.Number);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
// Methode zum Protokollieren von Einträgen in eine Log-Datei
|
||||
static void LogEntries(string filePath, List<string> entries)
|
||||
{
|
||||
if (entries.Count > 0)
|
||||
{
|
||||
File.AppendAllLines(filePath, entries);
|
||||
}
|
||||
}
|
||||
|
||||
// Klasse zur Darstellung eines Nutzers
|
||||
class User
|
||||
{
|
||||
public string Gender { get; set; }
|
||||
public string Firstname { get; set; }
|
||||
public string Lastname { get; set; }
|
||||
public string Birthdate { get; set; }
|
||||
public Address Address { get; set; }
|
||||
public string Email { get; set; }
|
||||
public List<Phone> Phones { get; set; } = new List<Phone>();
|
||||
}
|
||||
|
||||
// Klasse zur Darstellung einer Adresse
|
||||
class Address
|
||||
{
|
||||
public string Street { get; set; }
|
||||
public string PostalCode { get; set; }
|
||||
public string City { get; set; }
|
||||
}
|
||||
|
||||
// Klasse zur Darstellung einer Telefonnummer
|
||||
class Phone
|
||||
{
|
||||
public string Prefix { get; set; }
|
||||
public string Number { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Unser 1. Versuch
|
||||
|
||||
/*
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
@ -111,246 +363,4 @@ class Program
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Das hier wäre eine Lösungsvariante. Diese beruht auf den vorhandenen Daten
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Connection zum Server
|
||||
string myConnectionString = "server=localhost;uid=root;pwd=root;database=import_export";
|
||||
|
||||
Console.WriteLine("Willkommen");
|
||||
Console.Write("Bitte geben Sie nun die Kundennummer ein: ");
|
||||
string kundennummer = Console.ReadLine();
|
||||
Console.Write("Bitte geben Sie die Datei-Parameter ein: ");
|
||||
string dateiBezeichnung = Console.ReadLine();
|
||||
|
||||
string filePath = "path/to/your/" + dateiBezeichnung; // Adjust the path to your CSV file
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
Console.WriteLine("Die Datei-Parameter konnten nicht gefunden werden. Bitte versuche es erneut");
|
||||
return;
|
||||
}
|
||||
|
||||
List<string> invalidEntries = new List<string>();
|
||||
List<string> existingEntries = new List<string>();
|
||||
|
||||
// Einlesen der CSV-Datei und Verarbeitung der Daten
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
List<User> users = ParseCsv(lines, invalidEntries);
|
||||
|
||||
using (MySqlConnection myConnection = new MySqlConnection(myConnectionString))
|
||||
{
|
||||
myConnection.Open();
|
||||
|
||||
foreach (User user in users)
|
||||
{
|
||||
// Überprüfen, ob der Nutzer bereits existiert
|
||||
if (UserExists(myConnection, user, kundennummer))
|
||||
{
|
||||
existingEntries.Add($"{user.Firstname} {user.Lastname}, {user.Email}");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Nutzer einfügen
|
||||
InsertUser(myConnection, user, kundennummer);
|
||||
}
|
||||
}
|
||||
|
||||
// Ungültige und bereits vorhandene Einträge protokollieren
|
||||
LogEntries("invalid_entries.log", invalidEntries);
|
||||
LogEntries("existing_entries.log", existingEntries);
|
||||
|
||||
Console.WriteLine("Import abgeschlossen.");
|
||||
}
|
||||
|
||||
static List<User> ParseCsv(string[] lines, List<string> invalidEntries)
|
||||
{
|
||||
List<User> users = new List<User>();
|
||||
User currentUser = null;
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string[] parts = line.Split(';');
|
||||
|
||||
if (parts[0] == "Herr" || parts[0] == "Frau")
|
||||
{
|
||||
if (currentUser != null)
|
||||
{
|
||||
users.Add(currentUser);
|
||||
}
|
||||
currentUser = new User
|
||||
{
|
||||
Gender = parts[0],
|
||||
Firstname = parts[1],
|
||||
Lastname = parts[2],
|
||||
Birthdate = parts[3]
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parts[0] == "Adresse" && currentUser != null)
|
||||
{
|
||||
currentUser.Address = new Address
|
||||
{
|
||||
Street = parts[1],
|
||||
PostalCode = parts[2],
|
||||
City = parts[3]
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parts[0] == "E-Mail" && currentUser != null)
|
||||
{
|
||||
currentUser.Email = parts[1];
|
||||
if (!IsValidEmail(currentUser.Email))
|
||||
{
|
||||
invalidEntries.Add($"Ungültige E-Mail: {currentUser.Email}");
|
||||
currentUser = null;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parts[0] == "Telefon" && currentUser != null)
|
||||
{
|
||||
if (!IsValidPhone(parts[1], parts[2]))
|
||||
{
|
||||
invalidEntries.Add($"Ungültige Telefonnummer: {parts[1]} {parts[2]}");
|
||||
currentUser = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentUser.Phones.Add(new Phone
|
||||
{
|
||||
Prefix = parts[1],
|
||||
Number = parts[2]
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentUser != null)
|
||||
{
|
||||
users.Add(currentUser);
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
static bool IsValidEmail(string email)
|
||||
{
|
||||
try
|
||||
{
|
||||
var addr = new System.Net.Mail.MailAddress(email);
|
||||
return addr.Address == email;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool IsValidPhone(string prefix, string number)
|
||||
{
|
||||
return Regex.IsMatch(prefix, @"^\d{3,5}$") && Regex.IsMatch(number, @"^\d{4,10}$");
|
||||
}
|
||||
|
||||
static bool UserExists(MySqlConnection connection, User user, string clientId)
|
||||
{
|
||||
string query = @"SELECT COUNT(*) FROM user WHERE firstname = @firstname AND lastname = @lastname AND clientId = @clientId";
|
||||
MySqlCommand cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@firstname", user.Firstname);
|
||||
cmd.Parameters.AddWithValue("@lastname", user.Lastname);
|
||||
cmd.Parameters.AddWithValue("@clientId", clientId);
|
||||
return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
|
||||
}
|
||||
|
||||
static void InsertUser(MySqlConnection connection, User user, string clientId)
|
||||
{
|
||||
string query = @"INSERT INTO user (clientId, genderId, firstname, lastname, birthdate) VALUES (@clientId, @genderId, @firstname, @lastname, @birthdate)";
|
||||
MySqlCommand cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@clientId", clientId);
|
||||
cmd.Parameters.AddWithValue("@genderId", user.Gender == "Herr" ? 1 : (user.Gender == "Frau" ? 2 : 3));
|
||||
cmd.Parameters.AddWithValue("@firstname", user.Firstname);
|
||||
cmd.Parameters.AddWithValue("@lastname", user.Lastname);
|
||||
cmd.Parameters.AddWithValue("@birthdate", DateTime.ParseExact(user.Birthdate, "dd.MM.yyyy", CultureInfo.InvariantCulture));
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
long userId = cmd.LastInsertedId;
|
||||
|
||||
if (user.Address != null)
|
||||
{
|
||||
query = @"INSERT INTO address (userId, street, postalcode, city) VALUES (@userId, @street, @postalcode, @city)";
|
||||
cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@userId", userId);
|
||||
cmd.Parameters.AddWithValue("@street", user.Address.Street);
|
||||
cmd.Parameters.AddWithValue("@postalcode", user.Address.PostalCode);
|
||||
cmd.Parameters.AddWithValue("@city", user.Address.City);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(user.Email))
|
||||
{
|
||||
query = @"INSERT INTO email (userId, email) VALUES (@userId, @Email)";
|
||||
cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@userId", userId);
|
||||
cmd.Parameters.AddWithValue("@Email", user.Email);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
foreach (var phone in user.Phones)
|
||||
{
|
||||
query = @"INSERT INTO phone (userId, phoneprefix, phonenumber) VALUES (@userId, @phoneprefix, @phonenumber)";
|
||||
cmd = new MySqlCommand(query, connection);
|
||||
cmd.Parameters.AddWithValue("@userId", userId);
|
||||
cmd.Parameters.AddWithValue("@phoneprefix", phone.Prefix);
|
||||
cmd.Parameters.AddWithValue("@phonenumber", phone.Number);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
static void LogEntries(string filePath, List<string> entries)
|
||||
{
|
||||
if (entries.Count > 0)
|
||||
{
|
||||
File.AppendAllLines(filePath, entries);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class User
|
||||
{
|
||||
public string Gender { get; set; }
|
||||
public string Firstname { get; set; }
|
||||
public string Lastname { get; set; }
|
||||
public string Birthdate { get; set; }
|
||||
public Address Address { get; set; }
|
||||
public string Email { get; set; }
|
||||
public List<Phone> Phones { get; set; } = new List<Phone>();
|
||||
}
|
||||
|
||||
class Address
|
||||
{
|
||||
public string Street { get; set; }
|
||||
public string PostalCode { get; set; }
|
||||
public string City { get; set; }
|
||||
}
|
||||
|
||||
class Phone
|
||||
{
|
||||
public string Prefix { get; set; }
|
||||
public string Number { get; set; }
|
||||
}
|
||||
|
||||
*/
|
Loading…
Reference in New Issue
Block a user