Home > Mobile >  how to get the top 10 of the day C#
how to get the top 10 of the day C#

Time:02-15

I have a table in SQL server:

| date_Tim| Machine|Case_wrong|

|:---------|:--------:|:----------:|

|07/03/21 16:53:PM|Test1|1|

|07/03/21 16:58:PM|Test1|1|

|07/03/21 16:59:PM|Test1|1|

|07/03/21 16:58:PM|Test2|1|

|07/03/21 16:59:PM|Test2|1|

|07/03/21 17:00:PM|Test2|1|

|07/03/21 17:01:PM|Test3|1|

|08/03/21 16:58:PM|Test3|1|

|08/03/21 16:58:PM|Test2|1|

I want to sum column machine All machine from date 07/03/22 and fill to chart I try code

  private void loadchart()
        {
            var Today = DateTime.Now.ToString("dd/MM/yy");
            ChartTop.Series[0].Points.Clear();
            ChartTop.ChartAreas["ChartArea1"].AxisX.Interval = 1;
            string constring = ConfigurationManager.ConnectionStrings["Connstring"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.Connection = con;
            sqlCmd.CommandType = CommandType.Text;
            SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
            try
            {
                sqlCmd.CommandText = sqlCmd.CommandText = "SELECT TOP 10 Machine, Sum(Case_wrong) as Case_wrong FROM tbl_Count_"   cbWorkcell.Text   " group by Machine order by SUM(Case_wrong)";
                DataSet dtRecord = new DataSet();
                sqlDataAdap.Fill(dtRecord);
                ChartTop.DataSource = dtRecord;
                //set the member of the chart data source used to data bind to the X-values of the series  
                ChartTop.Series["Series1"].XValueMember = "Machine";
                //set the member columns of the chart data source used to data bind to the X-values of the series  
                ChartTop.Series["Series1"].YValueMembers = "Case_wrong";
             //   ChartTop.Series[0].ChartType = SeriesChartType.Pie;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

it works but it takes all the data in my table for calculation. is there a way to filter the data by date and sum there? Please help me! Thanks.

CodePudding user response:

What appears is that date_Tim is a datetime feild , if thats the case then you need a where clause added to your query , I am assuming that the date you mentioned (07/03/22) is DD/MM/YY based on that the query needs to be updated to add the clause as below

"SELECT TOP 10 Machine, Sum(Case_wrong) as Case_wrong FROM tbl_Count_"   cbWorkcell.Text   " WHERE date_Tim >='2022-03-07 00:00:00.000'  group by Machine order by SUM(Case_wrong)"

Please note @madreflection has pointed out critical errors, please fix them more details about SQL injection here

  • Related