Как определить параметры mysql min (date) и max (date) перед циклом - PullRequest
0 голосов
/ 15 апреля 2019

Я наполовину новичок в C # и никогда не храню объекты из запроса MySQL.В этом случае я объявил 2 переменные Datetimes:

DateTime date_min
DateTime date_max

Я бы хотел сохранить mysql Parameters max (date) и min (date) до запроса lopp снизу:

string query1 = "SELECT MIN(order_status.BEGIN_DATE) AS 'BEGIN DATE', MAX(order_status.END_DATE) AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";

я бы попробовал вот так, например:

SqlParameter param  = new SqlParameter();
param.ParameterName = "@max_date";
param.Value         = input_max_date;

Тогда я хочу использовать вот так:

string query1 = "SELECT @min_date AS 'BEGIN DATE', @max_date AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";

из-за:

@min_date = MIN(order_status.BEGIN_DATE)
@max_date = MAX(order_status.END_DATE)

тогда у меня есть цикл:

for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
    {
        var cmd = new MySqlCommand(query1, connection);

        string query2 = "SELECT COUNT(leave.ID_WORKER) AS 'NUMBER OF WORKERS ON LEAVE' FROM project1.leave WHERE DATE(leave.BEGIN_DATE) <= '"+date+"' AND DATE(leave.END_DATE) >= '"+date+"';";
        string query3 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ALL ORDERS IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"');";
        string query4 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ORDER IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER  = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"') AND orders.NUMBER_ORDER = 'TEST';";

using (var command = new MySqlCommand(query1, connection))
                {
                    using (var reader1 = command.ExecuteReader())
                    {
                        while (reader1.Read())
                        {
                            date_min = Convert.ToDateTime(reader1[0]);
                            date_max = Convert.ToDateTime(reader1[1]);
                        }
                    }
                }

                var cmd1 = new MySqlCommand(query2, connection);
                var cmd2 = new MySqlCommand(query3, connection);
                var cmd3 = new MySqlCommand(query4, connection);
    }

Может кто-нибудь показать, какие строки кода мне следует написать?может быть cmd.Parameters достаточно?THX за любую помощь?

Есть мой код:

 public partial class GenerateChartsOfOrders : Form
    {
        DateTime date_min;
        DateTime date_max;
        public GenerateChartsOfOrders()
        {
            InitializeComponent();
        }
        public void loaddata2()
        {
string query1 = "SELECT MIN(order_status.BEGIN_DATE) AS 'BEGIN DATE', MAX(order_status.END_DATE) AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";

            for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
            {   
                var cmd = new MySqlCommand(query1, connection);

                string query2 = "SELECT COUNT(leave.ID_WORKER) AS 'NUMBER OF WORKERS ON LEAVE' FROM project1.leave WHERE DATE(leave.BEGIN_DATE) <= '"+date+"' AND DATE(leave.END_DATE) >= '"+date+"';";
                string query3 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ALL ORDERS IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"');";
                string query4 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ORDER IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER  = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"') AND orders.NUMBER_ORDER = 'TEST';";

using (var command = new MySqlCommand(query1, connection))
                    {
                        using (var reader1 = command.ExecuteReader())
                        {
                            while (reader1.Read())
                            {
                                date_min = Convert.ToDateTime(reader1[0]);
                                date_max = Convert.ToDateTime(reader1[1]);
                            }
                        }
                    }

                    var cmd1 = new MySqlCommand(query2, connection);
                    var cmd2 = new MySqlCommand(query3, connection);
                    var cmd3 = new MySqlCommand(query4, connection);
            }
        }
    }
...