[MySQLGuide] Ferdig kode brukt i guide

This commit is contained in:
Alex Thomassen 2019-02-14 12:17:14 +01:00
parent 26fe008193
commit acc3dc5a86
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE
3 changed files with 61 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -28,20 +28,47 @@
/// </summary>
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;
}
}

View File

@ -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();
}
}
}