diff --git a/Database.cs b/Database.cs index 1f4dee5..d1db7dd 100644 --- a/Database.cs +++ b/Database.cs @@ -7,8 +7,9 @@ namespace it_projekt { class Database { + private static Database _database; public List Persons { get; set; } - public Database(String username, String password) + private Database(String username, String password) { string connectionString = "server=localhost;port=3306;database=stammdaten;User Id=" + username + ";Password=" + password + ";"; MySqlConnection connection = new MySqlConnection(connectionString); @@ -36,5 +37,14 @@ namespace it_projekt connection.Close(); Console.WriteLine("Done."); } + + public static Database getDatabase(string username, string password) + { + if (_database == null) + { + _database = new Database(username, password); + } + return _database; + } } } diff --git a/Form1.cs b/Form1.cs index 96a42ec..c424c8c 100644 --- a/Form1.cs +++ b/Form1.cs @@ -15,6 +15,7 @@ namespace it_projekt public Form1() { InitializeComponent(); + this.exportBtn.Enabled = false; } @@ -29,19 +30,23 @@ namespace it_projekt private void loginBtn_Click(object sender, EventArgs e) { LoadUsersFromTable(this.userTxt.Text, this.passTxt.Text); + } public void LoadUsersFromTable(string user, string password) { - if (db == null) + db = Database.getDatabase(user, password); + if (db.Persons == null) { - db = new Database(user, password); + MessageBox.Show("Connection to Database failed! \nPlease Try again.", "Database Error!"); } - foreach (Person person in db.Persons) - { - this.dataGrid.Rows.Add(person.Id, person.Firstname, person.Lastname, person.department_short, person.department_long, person.CreationDate, false); + else { + foreach (Person person in db.Persons) + { + this.dataGrid.Rows.Add(person.Id, person.Firstname, person.Lastname, person.department_short, person.department_long, person.CreationDate, false); + } } }