MVC-databaskonstruktion

Log | Files | Refs | README

commit 9c3a0e2ee5224bb4e61ceaaa069cd1068d8eb47e
parent dcb19040b567f246bba08761198e159f9f3f695b
Author: William Lindholm <a22willi@student.his.se>
Date:   Mon,  9 Oct 2023 11:06:46 +0200

Implemented databaseconnection, fetches agents.

Diffstat:
MMVC-databaskonstruktion/Controllers/AgentsController.cs | 18++++++++++++++++++
AMVC-databaskonstruktion/Models/AgentsModel.cs | 31+++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/MVC-databaskonstruktion/Controllers/AgentsController.cs b/MVC-databaskonstruktion/Controllers/AgentsController.cs @@ -1,12 +1,30 @@ using Microsoft.AspNetCore.Mvc; +using MVC_databaskonstruktion.Models; +using System.Data; namespace MVC_databaskonstruktion.Controllers { public class AgentsController : Controller { + private readonly IConfiguration _configuration; + private AgentsModel _agentsModel; + + public AgentsController(IConfiguration configuration) + { + this._configuration = configuration; + } + public IActionResult Index() { + GetAgents(); return View(); } + + private void GetAgents() + { + _agentsModel = new AgentsModel(this._configuration); + DataTable agents = _agentsModel.GetAgents(); + ViewBag.Agents = agents; + } } } diff --git a/MVC-databaskonstruktion/Models/AgentsModel.cs b/MVC-databaskonstruktion/Models/AgentsModel.cs @@ -0,0 +1,31 @@ +using MySql.Data.MySqlClient; +using Org.BouncyCastle.Security; +using System.Data; + +namespace MVC_databaskonstruktion.Models +{ + public class AgentsModel + { + private IConfiguration _configuration; + private string connectionString; + + public AgentsModel(IConfiguration configuration) + { + _configuration = configuration; + this.connectionString = _configuration["ConnectionString"]; + } + + public DataTable GetAgents() + { + MySqlConnection dbcon = new MySqlConnection(connectionString); + dbcon.Open(); + MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM Agent;", dbcon); + DataSet ds = new DataSet(); + adapter.Fill(ds, "result"); + DataTable agentsTable = ds.Tables["result"]; + dbcon.Close(); + + return agentsTable; + } + } +}