diff --git a/DatabaseConnection/DatabaseConnection/App.config b/DatabaseConnection/DatabaseConnection/App.config index 731f6de..9928048 100644 --- a/DatabaseConnection/DatabaseConnection/App.config +++ b/DatabaseConnection/DatabaseConnection/App.config @@ -3,4 +3,7 @@ + + + \ No newline at end of file diff --git a/DatabaseConnection/DatabaseConnection/DatabaseConnection.csproj b/DatabaseConnection/DatabaseConnection/DatabaseConnection.csproj index 5d38996..3616b2e 100644 --- a/DatabaseConnection/DatabaseConnection/DatabaseConnection.csproj +++ b/DatabaseConnection/DatabaseConnection/DatabaseConnection.csproj @@ -49,6 +49,7 @@ ..\..\..\..\..\..\..\Program Files (x86)\MySQL\Connector NET 8.0\Assemblies\v4.5.2\MySql.Web.dll + diff --git a/DatabaseConnection/DatabaseConnection/DatabaseConnectionForm.Designer.cs b/DatabaseConnection/DatabaseConnection/DatabaseConnectionForm.Designer.cs index d44f175..0944fb8 100644 --- a/DatabaseConnection/DatabaseConnection/DatabaseConnectionForm.Designer.cs +++ b/DatabaseConnection/DatabaseConnection/DatabaseConnectionForm.Designer.cs @@ -37,19 +37,17 @@ // tableData // this.tableData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.tableData.Location = new System.Drawing.Point(16, 15); - this.tableData.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.tableData.Location = new System.Drawing.Point(12, 12); this.tableData.Name = "tableData"; - this.tableData.Size = new System.Drawing.Size(1124, 473); + this.tableData.Size = new System.Drawing.Size(843, 384); this.tableData.TabIndex = 0; // // updateBtn // this.updateBtn.Cursor = System.Windows.Forms.Cursors.Hand; - this.updateBtn.Location = new System.Drawing.Point(523, 511); - this.updateBtn.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.updateBtn.Location = new System.Drawing.Point(392, 415); this.updateBtn.Name = "updateBtn"; - this.updateBtn.Size = new System.Drawing.Size(175, 28); + this.updateBtn.Size = new System.Drawing.Size(131, 23); this.updateBtn.TabIndex = 1; this.updateBtn.Text = "Oppdater tabell"; this.updateBtn.UseVisualStyleBackColor = true; @@ -57,14 +55,15 @@ // // DatabaseConnectionForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1156, 554); + this.ClientSize = new System.Drawing.Size(867, 450); this.Controls.Add(this.updateBtn); this.Controls.Add(this.tableData); this.Cursor = System.Windows.Forms.Cursors.Default; + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.MaximizeBox = false; this.Name = "DatabaseConnectionForm"; this.Text = "Database Connection"; this.Load += new System.EventHandler(this.DatabaseConnectionForm_Load); diff --git a/DatabaseConnection/DatabaseConnection/MySqlConnectionHandler.cs b/DatabaseConnection/DatabaseConnection/MySqlConnectionHandler.cs index 59702dd..9130e5c 100644 --- a/DatabaseConnection/DatabaseConnection/MySqlConnectionHandler.cs +++ b/DatabaseConnection/DatabaseConnection/MySqlConnectionHandler.cs @@ -1,5 +1,6 @@ using System.Data; using MySql.Data.MySqlClient; +using System.Configuration; namespace DatabaseConnection { @@ -17,10 +18,19 @@ namespace DatabaseConnection // Sjekk at connection er definert. if (_connection == null) { - _connection = new MySqlConnection("server=localhost;user id=student_readonly;database=student2018"); + // Se i filen `App.config` om connection string stemmer. + // Dette ble egentlig bare brukt fordi jeg utviklet på to forskjellige maskiner med forskjellige + // MySQL servere og konfigurasjoner. "Default" bør være lignende: + // + // "server=localhost;port=3306;user id=root;password=passord;database=student2018" + // + // Alternativt så kan du bare endre denne stringen :) + var connectionString = ConfigurationManager.AppSettings["mysqlConnectionString"]; + _connection = new MySqlConnection(connectionString); } - // Pass på at connection er tilkoblet. + // Pass på at connection er tilkoblet, men ikke prøv å åpne en ny en + // hvis det allerede eksisterer en åpen connection. var state = _connection.State; if (state == ConnectionState.Closed) { @@ -39,14 +49,15 @@ namespace DatabaseConnection _connection.Close(); } - public DataTable GetTableValues() + public DataTable GetTableValues(bool withExtraValues = false) { // 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. - var data = new MySqlDataAdapter($"select * from student", _connection); + var sqlString = "SELECT * FROM student"; + var data = new MySqlDataAdapter(sqlString, _connection); // Fyller opp det midlertidige "DataTable"-variabelet med data // fra MySQL-databasen som vi akkurat hentet ut.