Tweak vindu og MySQL connection string i app.config

This commit is contained in:
Alex Thomassen 2019-01-17 17:09:52 +01:00
parent 9c9fc3912f
commit ea676a7354
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE
4 changed files with 27 additions and 13 deletions

View File

@ -3,4 +3,7 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="mysqlConnectionString" value="server=localhost;port=3306;user id=student_readonly;database=student2018" />
</appSettings>
</configuration>

View File

@ -49,6 +49,7 @@
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\MySQL\Connector NET 8.0\Assemblies\v4.5.2\MySql.Web.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />

View File

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

View File

@ -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.