From acc3dc5a86fa27be49f35276ccf4abd3dfcb51ff Mon Sep 17 00:00:00 2001 From: Alex Thomassen Date: Thu, 14 Feb 2019 12:17:14 +0100 Subject: [PATCH] [MySQLGuide] Ferdig kode brukt i guide --- .../DatabaseConnection.cs | 20 ++++++++++++++ .../MySQLDatabaseTilkobling/Form1.Designer.cs | 27 +++++++++++++++++++ .../MySQLDatabaseTilkobling/Form1.cs | 14 ++++++++++ 3 files changed, 61 insertions(+) diff --git a/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/DatabaseConnection.cs b/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/DatabaseConnection.cs index 15639fd..d0b322e 100644 --- a/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/DatabaseConnection.cs +++ b/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/DatabaseConnection.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; +using System.Data; namespace MySQLDatabaseTilkobling { @@ -27,5 +28,24 @@ namespace MySQLDatabaseTilkobling { connectionString.Close(); } + + public DataTable GetTableValues() + { + // Lag et variabel som holder et midlertidig "DataTable" + var table = new DataTable(); + + // MySqlDataAdapter lar oss sende SQL-kode til serveren og hente ut + // dataen vi vil ha fra databasen. + // `student` er navnet på tabellen som bruker i MySQL. Dette bør oppdateres om du bruker noe annet. + var data = new MySqlDataAdapter($"select * from student", connectionString); + + // Fyller opp det midlertidige "DataTable"-variabelet med data + // fra MySQL-databasen som vi akkurat hentet ut. + data.Fill(table); + + // Returnerer dataen som en "DataTable"-verdi + // Dette er ment til å brukes i "DataGridView" i form'et. + return table; + } } } diff --git a/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.Designer.cs b/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.Designer.cs index dd377b5..5abc10b 100644 --- a/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.Designer.cs +++ b/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.Designer.cs @@ -28,20 +28,47 @@ /// private void InitializeComponent() { + this.tableView = new System.Windows.Forms.DataGridView(); + this.refreshButton = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.tableView)).BeginInit(); this.SuspendLayout(); // + // tableView + // + this.tableView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.tableView.Location = new System.Drawing.Point(13, 13); + this.tableView.Name = "tableView"; + this.tableView.Size = new System.Drawing.Size(775, 389); + this.tableView.TabIndex = 0; + // + // refreshButton + // + this.refreshButton.Location = new System.Drawing.Point(316, 408); + this.refreshButton.Name = "refreshButton"; + this.refreshButton.Size = new System.Drawing.Size(137, 23); + this.refreshButton.TabIndex = 1; + this.refreshButton.Text = "Oppdater tabell"; + this.refreshButton.UseVisualStyleBackColor = true; + this.refreshButton.Click += new System.EventHandler(this.refreshButton_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.refreshButton); + this.Controls.Add(this.tableView); this.Name = "Form1"; this.Text = "MySQLDatabaseTilkobling"; + ((System.ComponentModel.ISupportInitialize)(this.tableView)).EndInit(); this.ResumeLayout(false); } #endregion + + private System.Windows.Forms.DataGridView tableView; + private System.Windows.Forms.Button refreshButton; } } diff --git a/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.cs b/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.cs index a6a79d8..b16f203 100644 --- a/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.cs +++ b/MySQLDatabaseTilkobling/MySQLDatabaseTilkobling/Form1.cs @@ -16,5 +16,19 @@ namespace MySQLDatabaseTilkobling { InitializeComponent(); } + + private void refreshButton_Click(object sender, EventArgs e) + { + DatabaseConnection database = new DatabaseConnection(); + + // Åpne en tilkobling til MySQL + database.OpenConnection(); + + // Hent ut data og sett dette inn i programvindu + tableView.DataSource = database.GetTableValues(); + + // Lukk tilkoblingen til MySQL + database.CloseConnection(); + } } }