Files
it-projekt/Database.cs

41 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace it_projekt
{
class Database
{
public List<Person> Persons { get; set; }
public Database(String username, String password)
{
string connectionString = "server=localhost;port=3306;database=stammdaten;User Id=" + username + ";Password=" + password + ";";
MySqlConnection connection = new MySqlConnection(connectionString);
try
{
connection.Open();
string sql = "SELECT * FROM stammdaten";
MySqlCommand cmd = new MySqlCommand(sql, connection);
MySqlDataReader rdr = cmd.ExecuteReader();
Persons = new List<Person>();
while (rdr.Read())
{
Person p = new Person(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString(), DateTime.Parse(rdr[3].ToString()));
Persons.Add(p);
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
connection.Close();
Console.WriteLine("Done.");
}
}
}