Mysql
This commit is contained in:
@@ -1,6 +1,116 @@
|
|||||||
namespace Nevyn.Classes
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
namespace Nevyn.Classes
|
||||||
{
|
{
|
||||||
public class Mysql
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Nevyn.Models;
|
using Nevyn.Models;
|
||||||
|
using Nevyn.Classes;
|
||||||
|
|
||||||
namespace Nevyn.Controllers
|
namespace Nevyn.Controllers
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -47,6 +47,12 @@ namespace Nevyn.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class updateFromDatabase
|
||||||
|
{
|
||||||
|
Classes.Mysql mysql = new Classes.Mysql();
|
||||||
|
// = mysql.selectWallet();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WalletDTO
|
public class WalletDTO
|
||||||
|
|||||||
@@ -23,6 +23,10 @@
|
|||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
|
<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="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.11" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||||
|
<PackageReference Include="MySql.Data" Version="8.0.32" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="MySql.Data" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -13,11 +13,10 @@
|
|||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "swagger",
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:8080",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
},
|
}
|
||||||
"applicationUrl": "http://localhost:8080",
|
|
||||||
"dotnetRunMessages": true
|
|
||||||
},
|
},
|
||||||
"IIS Express": {
|
"IIS Express": {
|
||||||
"commandName": "IISExpress",
|
"commandName": "IISExpress",
|
||||||
@@ -31,8 +30,7 @@
|
|||||||
"commandName": "Docker",
|
"commandName": "Docker",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||||
"publishAllPorts": true,
|
"environmentVariables": {}
|
||||||
"useSSL": true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user