In a Connection class.cs after entering my connection string, the Open.Connection and Close.Connection methods, I created the following method:
public void LibriList(Libro lista)
{
if (this.OpenConnection() == true)
try
{
string query = "SELECT * FROM libri_schema.libri";
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader rdr = cmd.ExecuteReader();
List<Libro> model = new List<Libro>();
while (rdr.Read())
{
var details = new Libro();
details.Titolo = rdr["Titolo"].ToString();
details.Autore = rdr["Autore"].ToString();
details.Editore = rdr["Editore"].ToString();
details.ISBN = rdr["ISBN"].ToString();
/* details.Prezzo = rdr["Prezzo"].ToString();
details.Pagine = rdr["Pagine"].ToString();
details.Quantità = rdr["Quantità"].ToString();*/
model.Add(details);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.CloseConnection();
}
this is my controller:
[HttpGet]
public ActionResult Libri()
{
return View();
}
//public ViewResult Registrationorm()
[HttpPost]
public ViewResult Libri(Libro Lista)
{
if (ModelState.IsValid)
{
var connection = new Connection();
connection.LibriList(Lista);
return View("Libri");
}
else
{
ViewBag.ErrorMessage = "Nessun Libro disponibile";
}
return View();
}
This is a Model:
public class Libro
{
public string Titolo { get; set; }
//string Autore da cambiare in Lista
public string Autore { get; set; }
public string Editore { get; set; }
public string ISBN { get; set; }
public int Pagine { get; set; }
public decimal Prezzo { get; set; }
public int Quantità { get; set; }
In mySQL Workbench I created a table with some books, how can I now view it through a view?
Dapper, your entire method can basically be shortened toreturn connection.Query<Libro>("SELECT Titolo, Autore, Editore, ISBN FROM libri_schema.libri");, which is much more maintainable and a lot less boilerplate code