Help with COUNT mysql and Asp Net Core

Asked

Viewed 158 times

0

In my system I have a form for opening calls, in which each call has the following status: Aberto, Agendado, Em Atendimento and Finalizado.

I would like to exhibit in home the amount of each one currently called, for this I created the following mysql script:

string sql = "SELECT STATUS, COUNT(STATUS) AS quantidade FROM chamados GROUP BY STATUS";

In mysql I get the correct return, for example :

3 Open
2 In attendance
1 Scheduled
1 Completed

My doubt now and how using . Net Core I can display this record in my home.

As requested by colleagues, follows my most detailed problem: In the print below I have these cards where I would like to display the count of records made with each status type:

Card da minha Home

The connection to the Database has already been made, makes the Insert, update and delete normally, the problem is on how to make my sql query return the counts and can display it in the Home.

In Model I used the following code (I just added the same query)

 public void Contagem()
    {
        string sql = "SELECT STATUS, COUNT(STATUS) AS quantidade FROM chamados GROUP BY STATUS";
    }

Then I need to record this in some variable and then display this variable in my Index.

          <div class="col-lg-3 col-xs-6">
      <div class="small-box bg-green">
        <div class="inner">
          <h3>40</h3> //Aqui iria retornar a quantidade de chamados em atendimento

          <p>Chamados em Atendimento</p>
        </div>
        <div class="icon">
          <i class="fa fa-headset"></i>
        </div>
        <a href="~/Chamados/Index" class="small-box-footer">Ver todos <i class="fa fa-arrow-circle-right"></i></a>
      </div>
    </div>

DAL file connecting to the database

using System.Data;
using MySql.Data.MySqlClient;

namespace HelpDesk.Uteis
{
    public class DAL 
    {
        private static string Server = "localhost";
        private static string Database = "helpdesk";
        private static string User = "root";
        private static string Password = "root";
        private static string ConnectionString = $"Server={Server};Database={Database};Uid={User};Pwd={Password};Sslmode=none;Charset=utf8;";

        private static MySqlConnection Connection;

        public DAL()
        {
            Connection = new MySqlConnection(ConnectionString);
            Connection.Open();
        }

        //Espera um parâmetro do tipo string 
        //contendo um comando SQL do tipo SELECT
        public DataTable RetDataTable(string sql)
        {
            DataTable data = new DataTable();
            MySqlCommand Command = new MySqlCommand(sql, Connection);
            MySqlDataAdapter da = new MySqlDataAdapter(Command);
            da.Fill(data);
            return data;
        }

        //Espera um parâmetro do tipo string 
        //contendo um comando SQL do tipo INSERT, UPDATE, DELETE
        public void ExecutarComandoSQL(string sql)
        {
            MySqlCommand Command = new MySqlCommand(sql, Connection);
            Command.ExecuteNonQuery();
        }
    }
}
  • Hello John, try to post what you have already managed to do, in your question we can’t even get the idea if you have already set up the database in your project. Hug.

  • João as Bruno Soares said no to know at what stage you are having doubts, try to clarify a little better. Hug.

  • @Brunosoares I edited my doubt trying to detail better. The connection to the database has already been made and works normally, I’m starting my studies in . net core and I am unable to create a logic to make a counting query return me the value in a variable that I can put in my index, thus displaying in this case the count of how many records were made in each status.

  • @Are you using any Orms? Is this query performed as? ADO.NET? Dapper? Entity Framework? With what you have put it is possible to know only that you have a string storing the query SQL

  • @Vinicius added my connection code, the SQL string I did was how I query the database to get the return of the record count done in each Status, if there is a better or more correct way to do this in . Net Core and can share can modify =)

1 answer

1


Make an object that receives this 2 information and then send it to the View

Model

public class ObjStatus
{
    public int quantidade{get; set;}
    public string status {get; set;}
}

Repository

public class RpoStatus 
{
    ...
    //Se for uma lista usar List<ObjStatus>, se for somente popular objeto usar ObjStatus e troque .ToList() para .FirstOrDefault()
    public List<ObjStatus> mtdListarStatus(){
        string sql = "SELECT STATUS, COUNT(STATUS) AS quantidade FROM chamados GROUP BY STATUS";
        return dbContext.Database.SqlServer<ObjStatus>(sql).ToList();
    }
}

Controller

ObjStatus obj = new ObjStatus();
RpoStatus rpo = new RpoStatus();
obj = rpo.mtdListarStatus();
return View(obj);

View (Edit as needed)

 @model.namespace.ObjStatus

// Se for uma lista, fazer o foreach, caso contrario desta forma:
<div class="inner">
    @Html.LabelFor(model => model.quantidade) 
    @Html.LabelFor(model => model.staus)
</div>

  • worked out, thank you so much for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.