This commit is contained in:
elias
2023-01-31 08:39:12 +01:00
parent 0872a55718
commit 63e25fcf84
5 changed files with 125 additions and 6 deletions

View File

@@ -1,6 +1,116 @@
namespace Nevyn.Classes
using MySql.Data.MySqlClient;
namespace Nevyn.Classes
{
public class Mysql
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
//Constructor
public Mysql()
{
Initialize();
}
//Initialize values
private void Initialize()
{
server = "192.168.1.108";
database = "nevyn";
uid = "nevyn";
password = "3edc4RFV";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Console.Write("Cannot connect to server. Contact administrator");
break;
case 1045:
Console.Write("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
Console.Write(ex.Message);
return false;
}
}
public Models.Wallet SelectWallet()
{
string query = "SELECT * FROM tblWallet";
//Create a list to store the result
Models.Wallet wallet = new Models.Wallet();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
wallet.Id = (int)dataReader["idtblWallet"];
wallet.Kort = (int)dataReader["Kort"];
wallet.Buffert = (int)dataReader["Buffert"];
wallet.Spara = (int)dataReader["Spara"];
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return wallet;
}
else
{
return wallet;
}
}
}
}

View File

@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Nevyn.Models;
using Nevyn.Classes;
namespace Nevyn.Controllers
{

View File

@@ -47,6 +47,12 @@ namespace Nevyn.Models
}
}
public class updateFromDatabase
{
Classes.Mysql mysql = new Classes.Mysql();
// = mysql.selectWallet();
}
}
public class WalletDTO

View File

@@ -23,6 +23,10 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="MySql.Data" Version="8.0.32" />
</ItemGroup>
<ItemGroup>
<None Remove="MySql.Data" />
</ItemGroup>
</Project>

View File

@@ -13,11 +13,10 @@
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:8080",
"dotnetRunMessages": true
}
},
"IIS Express": {
"commandName": "IISExpress",
@@ -31,8 +30,7 @@
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
"environmentVariables": {}
}
}
}